简体   繁体   English

即使增加了变量的值,它在while循环中也不会改变

[英]Even after incrementing the value of the variable it won't change in the while loop

    #include <stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    int main()
    {
    char s[] = "We promptly judged antique ivory buckles for the next prize";

    for(int i=0;i<strlen(s);i++)
    {
        s[i] = tolower(s[i]);
    }
    int i=0;
    int n=1;
    int counter =0;
    char *s1;
    s1=(char*)malloc(sizeof(char)*n);
    s1[0] = s[0];

    int k=0;
    while(s[i]!='\0')
    {

            for( k=0;k<strlen(s1);++k)
            {
                if(s[i] == s1[k] && s[i] !=' ')
                ++counter;


            }
            if(counter==0 && s[i]!=' ' )
            {
                ++n;

              s1 = realloc(s1, sizeof(char) * (n));
              ++k;
              printf("%d :  %d\n",n,k);
              s1[k] = s[i];
            }


        ++i;
        counter =0;
        k=0;
    }
    s1 =realloc(s1 , sizeof(char)*(n+1));
    s1[n] = '\0';
    printf("%s\n",s1);

    if(n == 26)
    printf("yes");
    else
    printf("No");

    return 0;
}

This program seeks to check whether the word is pangram or not.. It takes a letter from the array s and stores it in array s1 if the letter is not repeated in the latter. 该程序试图检查该单词是否为pangram。如果数组s1中没有重复字母,则从数组s中获取一个字母并将其存储在数组s1中。 Thus if the length of the array s1 after the while loop finishes is 26 it has all the letters possible. 因此,如果while循环结束后数组s1的长度为26,则它具有所有可能的字母。 But when i tried printing the s1 array it shows only w has its contents.. Confused to the extreme... 但是,当我尝试打印s1数组时,它只显示w的内容。

Your ++k (inside the if statement) is not needed, remove it and it will work: 不需要您的++k (在if语句中),将其删除即可使用:

    #include <stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    int main()
    {
    char s[] = "We promptly judged antique ivory buckles for the next prize";

    for(int i=0;i<strlen(s);i++)
    {
        s[i] = tolower(s[i]);
    }
    int i=0;
    int n=2;
    int counter =0;
    char *s1;
    s1=(char*)malloc(sizeof(char)*n);
    s1[0] = s[0];
    s1[1]='\0';

    int k=0;
    while(s[i]!='\0')
    {

            for( k=0;k<strlen(s1);++k)
            {
                if(s[i] == s1[k] && s[i] !=' ')
                    ++counter;

            }

            if(counter==0 && s[i]!=' ' )
            {
                ++n;

              s1 = realloc(s1, sizeof(char) * (n));
              /* ++k; */  /* <== REMOVE IT */
              printf("%d :  %d\n",n,k);
              s1[k] = s[i];
              s1[k+1]='\0';
            }


        ++i;
        counter =0;
        k=0;
    }
    s1 =realloc(s1 , sizeof(char)*(n+1));
    s1[n] = '\0';
    printf("%s\n",s1);

    if(n == 26)
    printf("yes");
    else
    printf("No");

    return 0;
}

k was already incremented at the end of the for statement, so the ++k later is the cause for the wrong result. kfor语句的末尾已经增加,因此以后的++k是导致错误结果的原因。 Also s1 should be initialized as NULL terminated, and should be kept NULL terminated inside the if statement, as without it s1 likely to contain garbage. 同样, s1应该初始化为NULL终止,并且应该在if语句中保持NULL终止,因为没有s1可能包含垃圾。

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

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