Thread: understanding functions

  1. #1
    desperate
    Guest

    Unhappy understanding functions

    I have to write a function that accepts a string. Coount how many vowels and how many other characters are in the string. The function will return these two counts. The function must be written using pointer notation and array notation. Please I need help understanding functions, pointers and arrays. The code I have so far is as follows:
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
    char InputString["I love programming"];
    numVowels(InputString);
    }
    numVowels(char arr[])
    {

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    22

    Smile

    Hi,
    This is the code.
    Don't forget to null terminate the string.
    Hoping you are happy.

    by
    A.Sebasti..
    -------------------------------------------------------------------------------
    #include <stdio.h>
    #include <string.h>
    int vow_con(char *input);

    int main()
    {
    char inputString[50];

    printf("\n Enter the String :");
    gets(inputString);
    inputString[strlen(inputString)]='\0'; /* Null Terminate the string*/
    vow_con(inputString);
    }

    int vow_con(char *input)
    {
    int vowCnt=0;
    int conCnt=0;
    int i=0;
    int others=0;

    while(input[i])
    { if((input[i]>='a')&&(input[i]<='z') ||(input[i]>='A') && (input[i]<='Z'))
    {
    if((input[i]=='a')||(input[i]=='A')||
    (input[i]=='e')||(input[i]=='E')||
    (input[i]=='i')||(input[i]=='I')||
    (input[i]=='o')||(input[i]=='O')||
    (input[i]=='u')||(input[i]=='U'))
    {
    vowCnt++;
    }
    else
    {
    conCnt++;
    }
    }
    i++;
    }
    printf("\n The Number of Vowels is : %d ",vowCnt);
    printf("\n The Number of Consonant is : %d ",conCnt);
    others=(strlen(input)-(vowCnt+conCnt));
    printf("\n The Number of Others is : %d ",others);
    }


    -----------------------------------------------------------------------------------
    if((input[i]>='a')&&(input[i]<='z') ||(input[i]>='A') && (input[

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    But what happens if the user enter 51 charecters?
    Code:
    char inputString[50];
    printf("\n Enter the String :");
    gets(inputString);

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As you might have already guessed, you can't input 51 characters if you only allocated 50 characters of memory in the array. You could reallocate your array to hold more elements if needed or you could use make the array hold more elements in the first place. You last option (you'll need this anyway) is have the program determine how many characters an input string is and truncate anything over your limit.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I would copy the string, convert to upper or lower case (to avoid the long if statement).

    toupper

    is the function, I think.

    Then use

    isalpha

    to test if it is a letter and then test to see if it is a vowel. If it is not a vowel it is a constant.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    The idea that you have suggested is good one.
    use it so that we can avoid the long if statement.
    And one more thing you cann't tell other than vowels are
    consonants because space,\ ,+,= and etc are not.
    This you can tell it others.

    By
    A.Sebasti...

  7. #7
    Unregistered
    Guest
    Hi!

    Sorry, but I have to say something about the following C code given by sebastiraj, because it's a misunderstanding of the C language.

    inputString[strlen(inputString)]='\0'; /* Null Terminate the string*/

    A C string is terminated by a '\0' character... That's right. But strlen depends on the '\0' character to compute the length of the string. The line above is interpreted as: search the first '\0' in an array of characters, and then overwrite it with a '\0'. Really strange if you ask me.

    The rest of sebastiraj's program looks ok.

    The problem is possibly the different behaviour fo the gets and fgets functions...

    gets(char *buf) reads a string from the stdin stream, up to an including a '\n' character and it REPLACES the '\n' by a '\0' character.

    fgets(FILE *fp, char *buf, int length) reads a string from the given stream (fp), stopping when either a '\n' character is found or the buffer (of length bytes) is full and it APPENDS the '\0' character. So there is two cases here: is the buffer was too small to read the '\n' character, the string does not contain the '\n' character, but IT IS terminated by a '\0'. If the buffer was big enough, then the last character of the string is '\n', and it is terminated by a '\0'. One often sees the following line, but technically it's not correct:

    fgets(stdin, buf, length); buf[strlen(buf)-1]='\0';

    This is often used to emulate the gets function, but without the possibility of overflows. The problem is that it is not always true that the last character of the string is the newline character '\n'. It's better to use this:

    fgets(stdin, buf, length); if(buf[strlen(buf)-1]=='\n') buf[strlen(buf)-1]='\0'; else while(getchar()!='\n');

    This gives you up to up to length-1 characters typed in, discarding the rest of the line if it was too long.

    Hope this helps to a better onderstanding of C.

    greetinx,
    alex

  8. #8
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Last message was mine... Don't know why it is 'Unregistered'.
    alex

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Thanks alex,
    I didn't even noticed the flaw in my code.
    Thanks a lot.


    By
    A.Sebasti.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  3. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM