简体   繁体   中英

C Program - Palindrome with functions - almost fully complete one small issue

Is there a way I can remove the white spaces when comparing two strings. The assignment is create a palindrome that is case insensitive and must ignore the white spaces. So far I have

void cmpNoCase(char str1[], char str2[]){
   if(strcasecmp(str1, str2)==0){
      printf ("%s is a palindrome.\n", str2);
   } else { ("%s is NOT a palindrome.\n", str2);
}
return;
}

and inside main I have this for loop to reverse the first inputted string from the user.

for (i=0, j= strlen(input2)-1; i < j; i++, j--){

   index=input2[i];
   input2[i]=input2[j];
   input2[j]=index;
}

Note: I have another function which compares the two strings with case sensitivity but takes spaces into account when comparing the index of the string (which is the entered palindrome). The only difference I have in the functions and their for loops are the 'strcmp' for the 1st function, the 2nd being 'strcasecmp' to ignore the case sensitivity.

you may try writing your own comparison function. Do like while(i<strlen(yourstr){// your logic for comparing the string character by character} more specifically while(i<strlen(yourstr)){char a=yourstr[i]; if(a=' ') {continue;}; // rest of your comparison code} otherwise you can do like: while((a=yourstring[i])!='\\n'){i++; if(a=' ') {continue;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM