Like Tree3Likes

help with longest and shortest WORD in c programming

This is a discussion on help with longest and shortest WORD in c programming within the C Programming forums, part of the General Programming Boards category; Please help me a rush program) needed badly The user will input a sentence. Your program will determine the longest ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    11

    help with longest and shortest WORD in c programming

    Please help mea rush program) needed badly
    The user will input a sentence. Your program will determine the longest word/s and the number of characters of the longest word. If the longest or shortest word occurs more than once, it will only output once.

    Example:
    input: life is so beautiful

    Output:
    Longest Word/s: beautiful
    Shortest Word/s: is,so
    Number of Characters: 9

    Input: the quick brown fox jumps over the lazy dog.
    Output:


    Longest Word/s: quick, brown, jumps
    Shortest Word/s: the fox dog
    Number of Characters: 5
    Last edited by tgeandpudding; 01-16-2013 at 06:41 AM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,099
    Unfortunately, there is no built-in function in C standard library which find the longest word given an input sentence. But you should be able to get something working using these standard functions:

    getchar - for reading characters
    isalpha - for testing whether something is a word character
    strcpy - for saving the words into a list
    strlen - for seeing which word is longest and how long it is

    Write out a simple pseudocode first in plain english like
    read a character
    is it a word character?
    if so, blah blah...
    etc.

    Then it reduces to a translation problem. Translate pseudocode to C. That's what the forum can help you with, not really writing it for you.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    11

    reply

    This program, how to improve this? It have invalid output. The output must be the question above. Sorry for a lot of question. I have only few idea about c programming
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    How much effort do you expect the members of this forum to put into helping you, when you show no effort yourself? People will be less inclined to help you if you're simply copying someone else's broken code.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Very sorry for that. I'm just making comparisons to programs of others to my program. Anyway thank you for the responses. If I post my real program can you help me Mr. Matticus?

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    11

    My real program

    Very sorry for that. I'm just making comparisons to programs of others to my program. Anyway thank you for the responses. If I post my real program can you help me Mr. Matticus?
    this is the real program that I made.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Very sorry for that. I'm just making comparisons to programs of others to my program. Anyway thank you for the responses. If I post my real program can you help me Mr. Matticus?
    this is the real program that I made.

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    So even though you copied this code, there is a glaring bug in this code that was not mentioned in the other thread so I will mention it here.

    This
    Code:
    #define LEN 20
    ...
         char word[LEN];
    ...
    ...
    
             while( (ch = getchar()) != '\n')
                    if(i < LEN)
                      word[i++] = ch;
    
             word[i]  = '\0'; /* OOPS if i == LEN (which is possible due to the above loop) you tromp over memory thats not yours (undefined behavior) */
    ...

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    Quote Originally Posted by tgeandpudding View Post
    Very sorry for that. I'm just making comparisons to programs of others to my program. Anyway thank you for the responses. If I post my real program can you help me Mr. Matticus?
    this is the real program that I made.
    Of course! That's why I'm here. Be sure to use code tags when posting your code, as well as any problems you're having.

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Okay. But what about my code. the attachment above? thanks for the help.. There is a lot of errors making my head aching.
    Last edited by tgeandpudding; 01-16-2013 at 08:08 AM.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Quote Originally Posted by Matticus View Post
    Of course! That's why I'm here. Be sure to use code tags when posting your code, as well as any problems you're having.
    I don't know how to use code tags. I am new here. Very sorry.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    In the future, it would be better to just post your code so it can be referenced easily.

    First of all, proper indentation is very important. Here is your code with better formatting:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void main()
    {
        char longestw, shortestw, temp,i;
        int longest = 0, shortest=0;
    
        while(temp=='\0')
        {
            scanf("%c",&temp);
            if(strlen(temp)>longest)
            {
                longestw=temp;
                longest=strlen(temp);
            }
    
            if(shortest==0)
            {
                shortest= strlen(temp);
                shortestw=temp;
            }
        }  // ???
            else(strlen(temp)<shortest)
            {
                shortest=strlen(temp);
                shortestw=temp;
            }
        }
    
        for(i=0; i<strlen(words);++i)
        {
            printf("%s",words[i]);
            if(i<strlen-1)
            {
                printf("%s,", word);
            }
            else
            {
                printf(".");
            }
        }
    
        printf("the longest word entered was %s with %d characters", longestw, longest);
        printf("the longest word entered was %s with %d characters", shortestw, shortest);
        getch();
        return 0;
    }
    Here's what I see so far:

    - With better indentation, it's clear that you have an extra closing curly brace on line 24, which is an error.
    - There's no reason to use "conio.h" - it is non-standard, and you're only using it for the "getch()" at the end of the program. This can accomplished easily with standard library functions.
    - "main()" return "int", not "void"
    - You need to use character arrays to store strings. You only have single character variables declared. This is insufficient to hold a string (which is a character array terminated with a null character, '\0').
    - The variable "temp" is uninitialized, and is used (in the condition of the "while()" loop) before it is ever given a value.
    - You're only reading a single character with "scanf()". You need to read a string. "scanf()" with "%s" will stop reading at the first whitespace, so this is only good for a single word. "fgets()" is probably better for this project.
    - You're using variables that are never declared ("words[]", "word").

    It seems clear that this code was written all at once without any intermediate testing (and perhaps with a hefty dose of copying). It is good practice to break you problem down into small steps, and tackle (and test) each one separately. And when you write code, compile often to make sure there are no mistakes along the way. Also, this is worth a read:

    A development process

    Also, a pencil and paper can go a long way in developing logic. Don't try to do it all in your head - figure it out by hand first, then use that to develop your code.

    I would recommend writing a program to read a string from the user and print it out to the screen. Compile and test. When it's working, add the next thing bit of code (say, to determine the longest word). Build up the program in steps.
    Last edited by Matticus; 01-16-2013 at 08:35 AM. Reason: more info
    tgeandpudding and Salem like this.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    Quote Originally Posted by tgeandpudding View Post
    I don't know how to use code tags. I am new here. Very sorry.
    It's not that hard, read the link.


    [code]
    (paste your code here)
    [/code]

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Quote Originally Posted by Matticus View Post
    In the future, it would be better to just post your code so it can be referenced easily.

    First of all, proper indentation is very important. Here is your code with better formatting:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void main()
    {
        char longestw, shortestw, temp,i;
        int longest = 0, shortest=0;
    
        while(temp=='\0')
        {
            scanf("%c",&temp);
            if(strlen(temp)>longest)
            {
                longestw=temp;
                longest=strlen(temp);
            }
    
            if(shortest==0)
            {
                shortest= strlen(temp);
                shortestw=temp;
            }
        }  // ???
            else(strlen(temp)<shortest)
            {
                shortest=strlen(temp);
                shortestw=temp;
            }
        }
    
        for(i=0; i<strlen(words);++i)
        {
            printf("%s",words[i]);
            if(i<strlen-1)
            {
                printf("%s,", word);
            }
            else
            {
                printf(".");
            }
        }
    
        printf("the longest word entered was %s with %d characters", longestw, longest);
        printf("the longest word entered was %s with %d characters", shortestw, shortest);
        getch();
        return 0;
    }
    Here's what I see so far:

    - With better indentation, it's clear that you have an extra closing curly brace on line 24, which is an error.
    - There's no reason to use "conio.h" - it is non-standard, and you're only using it for the "getch()" at the end of the program. This can accomplished easily with standard library functions.
    - "main()" return "int", not "void"
    - You need to use character arrays to store strings. You only have single character variables declared. This is insufficient to hold a string (which is a character array terminated with a null character, '\0').
    - The variable "temp" is uninitialized, and is used (in the condition of the "while()" loop) before it is ever given a value.
    - You're only reading a single character with "scanf()". You need to read a string. "scanf()" with "%s" will stop reading at the first whitespace, so this is only good for a single word. "fgets()" is probably better for this project.
    - You're using variables that are never declared ("words[]", "word").

    It seems clear that this code was written all at once without any intermediate testing (and perhaps with a hefty dose of copying). It is good practice to break you problem down into small steps, and tackle (and test) each one separately. And when you write code, compile often to make sure there are no mistakes along the way. Also, this is worth a read:

    A development process

    I would recommend writing a program to read a string from the user and print it out to the screen. Compile and test. When it's working, add the next thing bit of code (say, to determine the longest word). Build up the program in steps.

    A big thank you for your advice.

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Quote Originally Posted by Matticus View Post
    In the future, it would be better to just post your code so it can be referenced easily.

    First of all, proper indentation is very important. Here is your code with better formatting:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void main()
    {
        char longestw, shortestw, temp,i;
        int longest = 0, shortest=0;
    
        while(temp=='\0')
        {
            scanf("%c",&temp);
            if(strlen(temp)>longest)
            {
                longestw=temp;
                longest=strlen(temp);
            }
    
            if(shortest==0)
            {
                shortest= strlen(temp);
                shortestw=temp;
            }
        }  // ???
            else(strlen(temp)<shortest)
            {
                shortest=strlen(temp);
                shortestw=temp;
            }
        }
    
        for(i=0; i<strlen(words);++i)
        {
            printf("%s",words[i]);
            if(i<strlen-1)
            {
                printf("%s,", word);
            }
            else
            {
                printf(".");
            }
        }
    
        printf("the longest word entered was %s with %d characters", longestw, longest);
        printf("the longest word entered was %s with %d characters", shortestw, shortest);
        getch();
        return 0;
    }
    Here's what I see so far:

    - With better indentation, it's clear that you have an extra closing curly brace on line 24, which is an error.
    - There's no reason to use "conio.h" - it is non-standard, and you're only using it for the "getch()" at the end of the program. This can accomplished easily with standard library functions.
    - "main()" return "int", not "void"
    - You need to use character arrays to store strings. You only have single character variables declared. This is insufficient to hold a string (which is a character array terminated with a null character, '\0').
    - The variable "temp" is uninitialized, and is used (in the condition of the "while()" loop) before it is ever given a value.
    - You're only reading a single character with "scanf()". You need to read a string. "scanf()" with "%s" will stop reading at the first whitespace, so this is only good for a single word. "fgets()" is probably better for this project.
    - You're using variables that are never declared ("words[]", "word").

    It seems clear that this code was written all at once without any intermediate testing (and perhaps with a hefty dose of copying). It is good practice to break you problem down into small steps, and tackle (and test) each one separately. And when you write code, compile often to make sure there are no mistakes along the way. Also, this is worth a read:

    A development process

    Also, a pencil and paper can go a long way in developing logic. Don't try to do it all in your head - figure it out by hand first, then use that to develop your code.

    I would recommend writing a program to read a string from the user and print it out to the screen. Compile and test. When it's working, add the next thing bit of code (say, to determine the longest word). Build up the program in steps.


    Again thank you. I learn a lot of things today. Its already 11:00 am, I have classes tomorrow and the deadline of this code is tomorrow. Better luck next time. Next time I will do your advice so that I can make my code correctly.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Longest word
    By krakatao in forum C Programming
    Replies: 2
    Last Post: 03-09-2012, 01:25 PM
  2. longest word problem
    By zinzo in forum C Programming
    Replies: 3
    Last Post: 12-19-2010, 10:34 AM
  3. Longest and Shortest String
    By dnguyen1022 in forum C++ Programming
    Replies: 40
    Last Post: 01-31-2009, 08:08 PM
  4. Finding the longest word in a string.
    By whoami2 in forum C Programming
    Replies: 2
    Last Post: 04-14-2007, 11:00 AM
  5. using strlen and finding shortest and longest words
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-30-2001, 06:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21