注册 登录
编程论坛 C++教室

字符数组中是不是将引号‘替换掉之后,结束符号就没有意义了?

maojinlong 发布于 2015-01-23 12:30, 668 次点击
#include<stdio.h>
#include<string.h>
#include<ctype.h>


int main(void)
{
    char text[20]="";
   
    char ala[20];
    int shu=0;
    const char space=' ';
    const char quote='\'';
   
   
    printf("\nEnter a string,include space,comma:\n");
    fgets(text,50,stdin);
   
    printf("You entered is:%s  %d\n",text,strlen(text));


    for(int i=0;i<strlen(text);i++)
    {
        if(text[i]==quote||isalnum(text[i]))
            continue;
        text[i]=space;
    }
    printf("You entered is:%s  %d\n",text,strlen(text));
   text[strlen(text)]='\0';
   int index=0;

   while(text[index]!='\0')
    {
   
        if(text[index]==space)
            ++index;   
        shu=0;
        while(text[index]==quote||isalnum(text[index]))
        {
            
            ala[shu++]=text[index++];
        }
        ala[shu]='\0';
        printf("%s\n",ala);
   
    }
   
    return 0;
}
这是我的代码,结果运行正常。但是我输入的内容是不带引号的,而if(text[i]==quote||isalnum(text[i]))和while(text[index]==quote||isalnum(text[index]))这两句如果不带quote的话,前者不带是输出空格,后者不带的话无限循环。
我想的是不是假如我输入nihao,那么text[]={'n','i','h','a','o','\n','\0'},替换时,是不是把这里边的引号也算上了,要不然怎么会出现上述问题呢,求解答,没有不思考,昨天晚上都在想,也上网搜了,可是没找到答案。万分感谢!
1 回复
#2
maojinlong2015-01-23 12:42
我自己明白了,因为==优先级高于||,所以我自己多想了,sorry
1