Thread: please give me the algorithm or pseudocode

  1. #1
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38

    please give me the algorithm or pseudocode

    i want to make a program that will ask a sentence from the user and it will display the number of vowels found in the sentence.

    example...
    Enter sentence: i am a newbie of c programming.

    there are 3 a. there are 3 i. there are 0 u.
    there are 2 e. there are 1 o.

    help anyone? just the correct commands to type and i'll supply the others... PLEASE!!!!! ONEGAI!!!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Loop through each character of the string, test each character with against each vowel -- 'a', 'e', 'i', 'o', or 'u', if the character is the vowel, increment an associated counter that had been initialized originally to zero.

    Do you know how to input a string? You may want to check the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    please give me somthing more specific... like waht are the commands i'm going to use... and if possible, show me the code.. :-)

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    You will be using these keywords and operators: int main return char * [] for ++
    You will need these functions: puts fgets printf

    I think that's everything.
    Anyone see something I missed?

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Better idea. You show us some code and we will tell you where you screwed up.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Hope this help

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
       int c,u,a,e,o,i;
       u=a=e=o=i=0;
       char text[100];
       printf("Enter text please\n ");
       gets(text);
       for (c=0; c<=strlen(text); c++)
       {
         if ( text[c] =='u' || text[c] =='U') u++;
         else if (text[c] == 'a' || text[c] =='A') a++;
         else if (text[c] == 'e' || text[c] =='E') e++;
         else if (text[c] == 'o' || text[c] =='O') o++;
         else if (text[c] == 'i' || text[c] =='I') i++;
    
       }
       printf ("The result:\n");
       printf ("There are %d u\n",u);
       printf ("There are %d a\n",a);
       printf ("There are %d e\n",e);
       printf ("There are %d o\n",o);
       printf ("There are %d i\n",i);
       return 0;
    }
    Example:

    Enter text please
    I am a newbie of c programming
    The result:
    There are 0 u
    There are 3 a
    There are 2 e
    There are 2 o
    There are 3 i
    Last edited by Moony; 08-02-2006 at 02:55 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Algorithmically speaking, this is about the worst way you could control a loop iterating through a string:
    Code:
    for (c=0; c<=strlen(text); c++)
    strlen() has to count the number of characters in the string every time you iterate through the loop. Using text[c] to test if you've reached the nul-terminator is a much better control method.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by Salem
    If it is bad buffer why it is still there and in use?

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by itsme86
    Algorithmically speaking, this is about the worst way you could control a loop iterating through a string:
    Code:
    for (c=0; c<=strlen(text); c++)
    strlen() has to count the number of characters in the string every time you iterate through the loop. Using text[c] to test if you've reached the nul-terminator is a much better control method.
    Good to know, thanks.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > If it is bad buffer why it is still there and in use?
    Huh?
    Do not use gets(), EVER
    It's a fairly simple rule to obey.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>why it is still there

    http://en.wikipedia.org/wiki/Gets

    >>and in use?

    Well because people like yourself learn it and use it of course! I highly doubt that anyone
    getting paid to write code is using it, and any code it does use it is probably very, very old.

    gets: kind of like leaving the house, but locking yourself out - you cant get back in!
    Last edited by Richie T; 08-02-2006 at 04:15 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Thanks all. What about scanf()?
    Last edited by Moony; 08-03-2006 at 01:08 AM.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What about scanf() indeed. Are you asking if it's unsafe? If you misuse it it can be. Being smart/careful with the format string will keep it safe, though.
    If you understand what you're doing, you're not learning anything.

  15. #15
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Moony
    Thanks all. What about scanf()?

    Do a google on "secure programming in C" and you'll probably find a bunch of useful information on which C functions can be misused by the programmer and subsequently abused by the bad guys.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database Search Algorithm
    By Krupux in forum C Programming
    Replies: 1
    Last Post: 08-28-2003, 09:57 PM
  2. Help with an algorithm
    By PJYelton in forum C++ Programming
    Replies: 15
    Last Post: 06-11-2003, 10:25 AM
  3. Algorithm - Complexity.
    By visitant... in forum C Programming
    Replies: 5
    Last Post: 05-13-2003, 02:24 AM
  4. I need an algorithm.... please help
    By darcome in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 01:24 PM
  5. Algorithm question
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 10:52 AM