Thread: Need help.

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    21

    Need help.

    So I made this program, its purpose should be to for example I write a text and It needs to see how many words there are, how many vowels and so on...


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
    int i, length=0, count=0,length2=0,vowel=0,constan=0,num=0,sign=0;
    
    char hi[800];
    char hey[800];
    fgets (hi,800,stdin);
    
    printf("%s", hi);
    
    length= strlen(hi);
    
    printf("\nlength = %d",length);
    
    for (i=0; i<length; i++)
    {
        if (hi[i] == ' ') 
        {
        count++;
        }
        if (hi[i] !=' ')
        {
        hey[i]=hi[i];    
        }
    }
    
    length2=strlen(hey);
    printf("\nString hey  %s",hey);
    
    printf("\nlength 2= %d",length2);
    
    ++count;
    printf("\nNumber of words :%d",count);
    
    for (i=0; i<length2; i++)
    {
        if (hey[i] == 'a' || hey[i] == 'e'||hey[i] == 'i'||hey[i] == 'o'||hey[i] == 'u')
            {
            ++vowel;
            }
        else    
            {
            ++constan;
            }
    }
    for (i=0; i<length2; i++)
    {
        if(hey[i]>'0'&&hey[i]<'9')
            {
            ++num;
            }
        if(hey[i] == '!' || hey[i] == ','||hey[i] == '?'||hey[i] == '('||hey[i] == ')'||hey[i] == '‘'||hey[i] == '“')
            {
            ++sign;    
            }
    }
    
    printf("\nNumber of Vowels: %d",vowel);
    printf("\nNumber of Consonants: %d",constan);
    printf("\nNumber of numbers .-.:%d",num);
    printf("\nNumber of signs: %d",sign);
    printf("\n");
    }

    Now this here

    Code:
    for (i=0; i<length; i++)
    {
        if (hi[i] == ' ') 
        {
        count++;
        }
        if (hi[i] !=' ')
        {
        hey[i]=hi[i];    
        }
    }
    
    length2=strlen(hey);
    printf("\nString hey  %s",hey);
    
    printf("\nlength 2= %d",length2);
    Well I was hoping that it would create a string where there are no whitespace that is ' ', but everytime I run the program, the string hey just has few characters, why isnt every character except the ' ' from the first string saved in the other?

    Here is the text you can copy paste for the stdin, as an example (at small words, numbers its works ok I guess):

    "Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs."

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You can't just transfer the characters from hi to the exact same offset of hey since hey is not a copy of hi, but is missing characters (spaces). So you need to keep track of its offset separately:
    Code:
    int j = 0;
    for (int i = 0; i < length; i++)
        if (hi[i] == ' ')
            count++;
        else                 // use an else (don't test opposite condition)
            hey[j++] = hi[i];
    hey[j] = '\0'; // remember to zero terminate the string
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    Thank you

Popular pages Recent additions subscribe to a feed

Tags for this Thread