C 比较2个字符串并获取错误预期表达式
我正在做一个项目,你输入一个名字,然后打印姓名首字母。当我尝试比较字符串时,我得到一个“预期表达式”错误。我做错了什么C 比较2个字符串并获取错误预期表达式,c,string,compare,cs50,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 = GetStrin
#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");
}
}
}
#包括
#包括
#包括
#包括
#包括
内部主(空){
printf(“名称:”);
string name=GetString();
printf(“\n”);
int length=strlen(名称);
字符串compair1=“”;
对于(int l=0;l
- 如果你不想找到空间,你可以这样做:
#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");
}
}
#包括
#包括
#包括
#包括
内部主(空)
{
printf(“名称:”);
字符名[20];
获取(名称);
printf(“\n”);
int length=strlen(名称);
对于(int l=0;l
- 如果你不想找到空间,你可以这样做:
#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");
}
}
#包括
#包括
#包括
#包括
内部主(空)
{
printf(“名称:”);
字符名[20];
获取(名称);
printf(“\n”);
int length=strlen(名称);
对于(int l=0;l
您应该查找。下面是一个示例
#include <string.h>
#include <stdio.h>
int main()
{
char str[] = "Bufford T Justice";
char *token;
char space = ' ';
int count = 0;
token = strtok(str, &space);
while( token != NULL )
{
printf( "%s\n", token );
token = strtok(NULL, &space);
if( token )
{
count++;
}
}
printf("Number of spaces = %d\n", count);
return(0);
}
#包括
#包括
int main()
{
char str[]=“Bufford T Justice”;
字符*令牌;
字符空间=“”;
整数计数=0;
令牌=strtok(str和space);
while(令牌!=NULL)
{
printf(“%s\n”,标记);
token=strtok(NULL和空格);
如果(令牌)
{
计数++;
}
}
printf(“空格数=%d\n”,计数);
返回(0);
}
从这段代码中,只需几行就可以确定输入名称的首字母
注意:如果您不希望字符串被strok()
修改,您可以使用strchr()
进行一些小的更改。您应该查找。下面是一个示例
#include <string.h>
#include <stdio.h>
int main()
{
char str[] = "Bufford T Justice";
char *token;
char space = ' ';
int count = 0;
token = strtok(str, &space);
while( token != NULL )
{
printf( "%s\n", token );
token = strtok(NULL, &space);
if( token )
{
count++;
}
}
printf("Number of spaces = %d\n", count);
return(0);
}
#包括
#包括
int main()
{
char str[]=“Bufford T Justice”;
字符*令牌;
字符空间=“”;
整数计数=0;
令牌=strtok(str和space);
while(令牌!=NULL)
{
printf(“%s\n”,标记);
token=strtok(NULL和空格);
如果(令牌)
{
计数++;
}
}
printf(“空格数=%d\n”,计数);
返回(0);
}
从这段代码中,只需几行就可以确定输入名称的首字母
注意:如果您不希望字符串被strtok()
修改,您可以使用strchr()
进行一些小的更改。for(int l=0;name[l];l++){If(name[l]=''printf(“查找到的空格\n”);}
您不能只获取单个字符的地址
并将其用作strcmp
的参数,因为它不是以null结尾的。通常,这会导致分段冲突。如果您只想比较单个字符,可以使用int res=compair1[0]==compair2;
在“int res=strcmp(compair1,&compair2);@MarcusMardis&compair2的意思是“compair2的地址”。因此,您将向strcmp传递char变量的地址,strcmp将其视为以null结尾的字符串的开头,而它不是。您可以了解如何使用strcmp。for(int l=0;name[l];l++{if(name[l]='')printf“”;}
您不能只获取单个字符的地址,并将其用作strcmp
的参数,因为它不是以null结尾的。通常,这会导致分段冲突。如果您只想比较单个字符,可以使用int res=compair1[0]==compair2;
在“int res=strcmp(compair1,&compair2);@MarcusMardis&compair2的意思是“compair2的地址”。因此,您将向strcmp传递char变量的地址,strcmp将其视为以null结尾的字符串的开头,而不是。您可以了解如何使用strcmp。绝对正确。两个警告:1)不好。首选方案是fgets(mystring,len,stdin)
.2)使用将简化代码(并消除整个“for”循环)。+1因为不使用“cs50.h”;@palsm4:谢谢:)。绝对正确。两个警告:1)不好。首选方案是fgets(mystring,len,stdin)
.2)使用将简化代码(并消除整个“for”循环)。+1不使用“cs50.h”;@paulsm4:Thank:)。