Number of consonants

Discussion in 'C' started by answerx, May 23, 2011.

  1. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Help me please! I want to make a program that displays the word with the maximum number of consonants from a sentence.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How far have you got and where are you stuck?
    Can you create a program as a start that just displays individual words?
    After that you can count the number of consonants in each word and store (a) the number if it's the largest so far and (b) the word itself.
    At the end just display the word that you stored before.

    Possible algorithm:
    count=0; word=""
    for each word
    consonant count > count? yes: assign count and word
    end for
    display word.
     
  3. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Here is a variant of my program, please help me find the mistake.

    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <iostream.h>
    #include <string.h>
    
    
    void main()
    {char cons[21]={'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'};
    char s[30],x[30][30],z[30]; int i,j=0,k=0,r,f,n,poz,c,max;
    clrscr();
    printf("Type sentence:");
    gets(s);n=strlen(s);r=0;k=0;
    
    for(i=0;i<n;i++){
    if((s[i]!=' ')&&(s[i]!='.')){x[r][k]=s[i];k++;}
    if((s[i]==' ')||(s[i]=='.')){x[r][k]='\0';r++;k=0;}}
    
    for(i=0;i<r;i++)
    for(j=0;j<strlen(x[i]);j++)
    for(k=0;k<21;k++)
    if(x[j]=cons[k]) {c++;}
    z[i]=c;
    max=z[0]; poz=0;
    for(i=0;i<r;i++)
    if(max<z[i])
    {max=z[i];poz=i;}
    puts(x[poz]);}
     
    Last edited by a moderator: May 24, 2011
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I'm not surprised you can't debug that code; it's a horrendous mess. Use meaningful variable names, indent code correctly, and use code tags when posting here. You can identify code problems by adding printf statements to it that display intermediate results, for instance as I hinted you could try displaying words as you pull them out of the sentence, then display the consonant count and if it's greater than the previous greatest; these should all help you find the problems.

    Why is it "a variant on your program" and not your program itself? Did you find this example online instead of writing it yourself?
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your if statement contains an assignment. You probably wanted to do a comparison.
     
  6. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Of course I wroted myself, if I had found it on the internet it would be correct.
     
  7. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Yes it's a comparison, thanks for the correction. But it still doesn't work.:undecided
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    lol
     
  9. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    What can I say, I'm a beginner.:)
     
  10. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    A little help please!:shy:
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You can say: you've tried my suggestions and post the new (non-working) code WITH the changes. I'm not going to repeat myself as I have done previously.
     
  12. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Here is the new code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <iostream.h>
    #include <string.h>
    
    
    void main()
    {
    char cons[21]={'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'};
      char s[30],x[30][30],z[30]; int i,j=0,k=0,r,f,n,poz,c,max;
    clrscr();
    printf("Dati propozitia:");
    gets(s);n=strlen(s);r=0;k=0;
    
    for(i=0;i<n;i++)
    {
    if((s[i]!=' ')&&(s[i]!='.')){x[r][k]=s[i];k++;
    }
    if((s[i]==' ')||(s[i]=='.')){x[r][k]='\0';r++;k=0;
    }}
    
    for(i=0;i<r;i++)
    for(j=0;j<strlen(x[i]);j++)
    for(k=0;k<21;k++)
    if(x[j]==cons[k])
    {
    c++;
    }
    z[i]=c;
    max=z[0]; poz=0;
    for(i=0;i<r;i++)
    if(max<z[i])
    {
    max=z[i];poz=i;
    }
    puts(x[poz]);
    }
     
    Last edited by a moderator: May 25, 2011
  13. answerx

    answerx New Member

    Joined:
    May 23, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Is there somebody who can help me? Please, I'm desperate.
     
  14. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    OK, *one* repeat.

    1. create a program as a start that just displays individual words

    2. After that, count the number of consonants in each word and store (a) the number if it's the largest so far and (b) the word itself. Display these results as you calculate them.

    3. Use meaningful variable names

    4. Indent code correctly

    5. You can identify code problems by adding printf statements to it that display intermediate results and ensure that what the program is doing corresponds to what you think it is doing.

    You can almost certainly solve the problem yourself using these techniques.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice