Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中的字符比较_C_Pointers_Segmentation Fault_Character - Fatal编程技术网

c语言中的字符比较

c语言中的字符比较,c,pointers,segmentation-fault,character,C,Pointers,Segmentation Fault,Character,`我试着把句子中的每个单词都倒转过来,并且单词的位置是固定的。 while(c!='')seg的线路每次都会出错。有人能告诉我哪里出了问题吗?这是我的代码 #include<stdio.h> void swap(char *i, char *j) { char t; t = *i; *i = *j; *j = t; } void reverse(char *s, char *e) { char *i, *j; i = s;

`我试着把句子中的每个单词都倒转过来,并且单词的位置是固定的。 while(c!='')seg的线路每次都会出错。有人能告诉我哪里出了问题吗?这是我的代码

#include<stdio.h>

void swap(char *i, char *j)
{
    char t;
    t = *i;
    *i = *j;
    *j = t;
}

void reverse(char *s, char *e)
{
    char *i, *j;
    i = s;
    j = e;

    while(i <= j)
    {
        swap(i, j);
        i++;
        j--;
    }
}

int main()
{
    int check = 0;
    char *a= (char*) malloc(100*sizeof(char));
    char *c, *b, *t;
    char *s = ' ';
    printf("enter your sentence\n");
    fgets (a, 100, stdin);
    if ((strlen(a) > 0) && (a[strlen(a)-1] == '\n'))
        a[strlen(a)-1] = '\0'; 

    printf("\nyour stat: %s  and size is %d\n", a, strlen(a));

    b = a;
    c = a;

    while(*b != ' ')
        b++;

    b--;

    while(!check)
    {
        reverse(c, b);
        t = c;
        c = b;
        b = t;
        while(*c != ' ')// segmentation fault :|
            c++;
        while(*c == ' ')
            c++;

        b++;

        while(*b == ' ')
            b++;

        while((*b != ' ') && (*b != '\0'))
        {
            if(*b = '\0')
            {
                check = 1;
                b--; 
                reverse(c, b);
                break;
            } 
            b++;
        }

        b--;
    } 

    printf("\n reversed stat is : %s\n",a);
    return 0;
}
#包括
无效交换(字符*i,字符*j)
{
查尔特;
t=*i;
*i=*j;
*j=t;
}
无效反向(字符*s,字符*e)
{
char*i,*j;
i=s;
j=e;
而(i0)&(a[strlen(a)-1]='\n'))
a[strlen(a)-1]='\0';
printf(“\n您的状态:%s,大小为%d\n”,a,strlen(a));
b=a;
c=a;
而(*b!='')
b++;
b--;
同时(!检查)
{
反向(c,b);
t=c;
c=b;
b=t;
while(*c!=“”)//分段错误:|
C++;
而(*c=='')
C++;
b++;
而(*b=='')
b++;
而((*b!='')&&(*b!='\0'))
{
如果(*b='\0')
{
检查=1;
b--;
反向(c,b);
打破
} 
b++;
}
b--;
} 
printf(“\n反向状态为:%s\n”,a);
返回0;
}

您可以在一些地方改进代码,使其更加健壮。真正的问题是您正在使用

if(*b = '\0') // This assigns the null character to *b
而不是

if(*b == '\0') // This compare whether *b is the null character
更新

您可以采取哪些措施来改进代码:

  • 您可以使用数组,而不必使用
    malloc

    而不是

    char *a= (char*) malloc(100*sizeof(char));
    
  • 使用

  • 编写两个帮助函数来帮助跳过空白和非空白

    char* skipWhiteSpace(char* in)
    {
       while (isspace(*in) && *in != '\0') ++in;
       return in;
    }
    
    char* getEndOfNonWhiteSpace(char* in)
    {
       while (!isspace(*in) && *in != '\0') ++in;
       return in-1;
    }
    
    然后,可以简化
    main
    的核心<代码>主可以是:

    int main()
    {
       char a[100];
       char *c, *b;
       printf("enter your sentence\n");
       fgets (a, 100, stdin);
       if ((strlen(a) > 0) && (a[strlen(a)-1] == '\n'))
          a[strlen(a)-1] = '\0'; 
    
       printf("\nyour stat: %s  and size is %zu\n", a, strlen(a));
    
       c = a;
       while(1)
       {
          c = skipWhiteSpace(c);
          b = getEndOfNonWhiteSpace(c);
    
          reverse(c, b);
    
          c = b+1;
          if (*c == '\0' )
          {
             break;
          }
       } 
    
       printf("\n reversed stat is : %s\n",a);
       return 0;
    }
    

  • 失败的一行将愉快地继续通过字符串的末尾

    您还需要检查字符串的结尾:

        while(*c && *c != ' ') 
    
    if(*b='\0')
    更改为
    if(*b='\0')

    在这里:

     while((*b != ' ') && (*b != '\0'))
      {
                if(*b = '\0')  // here is problem change it with if(*b == '\0')
                {
                    check = 1;
                    b--; 
                    reverse(c, b);
                    break;
                } 
                b++;
      }
    
    您的此条件是将
    '\0'
    分配给
    *b
    ,不签入
    *b
    是否等于
    '\0
    。换成

    if(*b == '\0')
    

    几条建议

    • 分配的内存必须是free-d
    • 包括系统调用的头文件(malloc、strlen等)
    • 在比较中,使用与“变量值”比较的形式常量
    • 不要跳转或跳过单个字符,而是使用适当的系统调用:strrchr和strcat
    该版本还解决了不同平台上的换行问题

    #包括
    #包括
    #包括
    int
    main(int argc,char*argv[])
    {
    字符*语句;
    字符*间隙;
    字符*空格=”;
    字符*反转;
    大小;
    int len=0;
    if(-1==getline(&语句,&大小,stdin)){
    返回-1;
    }
    len=strlen(句子);
    /*换行符是最后一个字符吗*/
    gap=strrchr(句子“\n”);
    如果(空!=间隙){
    句子[--len]='\0';
    }
    /*回车是最后一个字符吗*/
    gap=strrchr(句子“\r”);
    如果(空!=间隙){
    句子[--len]='\0';
    }
    反向=malloc(len);
    if(NULL==反向){
    返回-2;
    }
    /*是否有最右边的空格字符*/
    while(NULL!=(gap=strrchr(句子“”)){
    /*把它变成一根绳子的末端*/
    *(gap++)='\0';
    /*将间隙后的“字”追加到反向缓冲区*/
    strcat(反向,间隙);
    strcat(反向“”);
    }
    /*最后一个“单词”也必须加上*/
    strcat(颠倒,句子);
    printf(“已反转:%s\n”,已反转);
    自由(反向);
    返回0;
    }
    
    请缩进代码,并在第7行加上随机引号。如果你想让别人回答的话,我希望我有125点,比如家庭作业,让我正确格式化你的代码。为什么不在你的调试器中逐步检查代码,看看出了什么问题?这将是一个比要求他人为您调试更快的解决方案,您将在这个过程中学到更多。使用比单个字母更好的变量名。和。这同样适用于
    中较高的位置,而(*b!='')
    if(*b = '\0')
    
    if(*b == '\0')