#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Longest word in array (C)


    Can someone help me to find a longest word in array ? (c)
    and print it ?
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by sasisken
    Can someone help me to find a longest word in array ? (c)
    and print it ?
    it seems pretty straightforward.

    what problem did you have?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
    int i,b,c,max=0,zodis=0;
    char raide;
    char line[50];
    printf("Type symbol line) \n");
    scanf("%[^\n]%*c", line);
    printf("Entered symbol line\n%s \n",line);
    for (i=0;line[i];i++)
    {
    if (line[i]!=' ')
    {
    I'm stuck here i dont know what to do next
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by sasisken
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
    int i,b,c,max=0,zodis=0;
    char raide;
    char line[50];
    printf("Type symbol line) \n");
    scanf("%[^\n]%*c", line);
    printf("Entered symbol line\n%s \n",line);
    for (i=0;line[i];i++)
    {
    if (line[i]!=' ')
    {
    I'm stuck here i dont know what to do next
    I don't see how that relates to finding the longest word in an array.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0
    Okey so how should i do it :/ ?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Figure out what it is that you need to do and then write the code to do it.

    Obviously, the program should involve an array of strings, which you will iterate through to determine which string is the longest.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0
    How to make that last word would be counted as well ?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {   char eil[50];
    int sk,i,ilgis=0,pradzia=0,maxpr=0,max=0;    
        printf("Iveskite simboliu eilute su tarpais  \n");
        gets(eil);  
       for (i=0; i < 50; i++)
           { 
                 sk = eil[i]; 
                 
                  if (sk!=' ') 
                 ilgis++;
                 
                  if (sk==' ')
               {   
                 if (max<ilgis) 
                    {max= ilgis;
                     
                     maxpr=pradzia;}
                     pradzia=i+1;
                  
                    ilgis=0;
                  
               }
            }  
      printf("Ilgiausias zodis:\n");    
         for (i=maxpr; i < maxpr+max; i++)
           printf("%c",eil[i]);  
           
     system("pause ");
     return 0;
    }
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Oh, so you wanted the longest word in a string! Why didn't you say so?

    consider this part of your code:
    Code:
            if (sk!=' ') 
                ilgis++;
                 
            if (sk==' ')
            {
    Instead, why don't you write that this way?:
    Code:
            if (sk == ' ') 
        {
            // your code for when you hit a delimiter between words
        }
        else
            ilgis++;
    That is how a programmer would normally write that.

    In addition, you can make the if test more complex without having to make the other test equally complex and properly negated. For example, besides testing for ' ', you could also test for punctuation marks, or the end of the string. By being able to detect the end of the string, you could "count the last word as well".

    You could simply keep your code as it is and change the tests in the two if statements, but then you'd have to perform those tests twice, plus you would be more likely to make a mistake in writing that duplicate code. Worse, if you find that you need to correct the if statement, then you might forget to also correct the other one (that's a maintainability issue). I would recommend simplifying your code with an if-else and then make the changes to the if statement's condition.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0
    Thanks and sorry for my bad grammar.

IMN logo majestic logo threadwatch logo seochat tools logo