Question: In the C code below, it is intended to check a source text file and determine if it is a palindrome or not. If it
In the C code below, it is intended to check a source text file and determine if it is a palindrome or not. If it is a palindrome, it should export to a file for palindromes and if it is not a palindrome, it should send to another file. All of that works great. However, I also need the code to change the first letter of all lines in the exported data to uppercase and the rest to lower case. This use to work, but when I added the newline array, it stopped converting the case. Can someone help with converting the case again? I'm at a lose.
#include
void modtext(char line[],char newLine[]); int isPalindrome(char line[]);
int main(int argc, char *argv[]) { if(argc!=2) { //Error if not filename is entered ****************** printf("Please enter filename after %s ", argv[0]); return -1; } //To open my file ******************************** FILE *fp; fp = fopen(argv[1], "r");
if(fp==NULL){ printf("Unable to open file "); return -1; } //To create the two output files that my program writes to****** FILE *fPal = fopen("palindromeOut.txt", "w"); FILE *fQuote = fopen("quotesOut.txt", "w");
char line[1000],newLine[1000];
while(!feof(fp)) { fscanf(fp, " %[^ ]s", line); modtext(line,newLine); int isPal = isPalindrome(newLine); line[0] = toupper(line[0]);
if(isPal){ //To print to palindromeOut.txt *************** fprintf(fPal, "%s ", line); //To print to screen ************************** printf("Palindrome:%s ", line ); }else{ //To print to quoteOut.txt ******************* fprintf(fQuote, "%s ", line); //To print to screen ************************ printf("Quote:%s ", line ); } }
//To close my files *************************** fclose(fp); fclose(fPal); fclose(fQuote);
return 0;
}
void modtext(char line[],char newLine[]){
int i=0,j=0; while(line[i]!='\0'){ if((line[i]>64&&line[i]<91)||(line[i]>96&&line[i]<123)) {
newLine[j]=line[i]; if((line[i]>64&&line[i]<91)) newLine[j]=newLine[j]+32; j++; } i++; } newLine[j]='\0'; }
int isPalindrome(char newLine[]){ int i, j, k, len=0; k = i; while(newLine[len]!='\0') len++; for(i=0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
