比较 2 个字符串并获得错误预期表达式

标签 c string compare cs50

我正在进行一个项目,您可以在其中输入姓名并打印首字母。当我尝试比较字符串时,出现“预期表达式”错误。我做错了什么?

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

int main(void) {
   printf("Name: ");
   string name = GetString();
   printf("\n");

   int length = strlen(name);
   string compair1 = " ";
   for(int l = 0;l<=length;l++) {
      char compair2 = name[l];
      int  res = strcmp(compair1,&compair2);
      if(res == 0) {
         printf("found blank space");
      }
   }
}

最佳答案

  • 如果你只是想找空间,那么你可以简单地这样做:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void)
    {
       printf("Name: ");
       char name[20];
       gets(name);
       printf("\n");
    
      int length = strlen(name);
      for(int l = 0;l < length;l++)
      {
          if(name[l] == ' ')
          printf("found blank space");
      }
    }
    

关于比较 2 个字符串并获得错误预期表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981305/

相关文章:

c - 棋盘游戏程序中的数组

iOS 应用程序 Localization.strings 文件名更改

java - 字符串十六进制编码和解码

python - 如何在Python中根据键和值合并两个字典?

c# - 多次比较字符串

c - C中的连续替换函数

c - 将矩阵提升为复数幂

python - 使用 kwargs 包装一个接受可选参数结构的函数

python - 在python中将str转换为unicode

javascript - 比较数组 Javascript 中索引元素内的数字