简体   繁体   English

C程序-具有功能的回文-几乎完全完成了一个小问题

[英]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. 在main内部,我有这个for循环来反转用户的第一个输入字符串。

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. 我在函数及其for循环中唯一的区别是第一个函数的“ strcmp”,第二个是“ strcasecmp”,以忽略大小写。

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;} 更具体地喜欢while(i<strlen(yourstr){// your logic for comparing the string character by character} while(i<strlen(yourstr)){char a=yourstr[i]; if(a=' ') {continue;}; // rest of your comparison code}否则,您可以执行以下操作: while((a=yourstring[i])!='\\n'){i++; if(a=' ') {continue;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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