You must Sign In to post a response.
  • Category: Programming

    C program to accept a string and count number of vowels in the string


    Stuck up in writing C program alongwith explanation for string operations? Looking out for the solution here? our experts shall provide you the proggram solution on this Ask Expert page.

    Following is the question asked in Nalanda Open University Bachelor in Computer Application (BCA), Part-I practical question paper-VI (C Programming and Data Structure (CS-62P) Set A) for 2015 Annual examination.
    This question is asked for 20 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program in C language which accepts a string as input and count the number of vowels in it. [20 Marks]
  • Answers

    1 Answers found.
  • In the given problem statement we have to accept the string and count the number of vowels in that string. There are two ways to accept the string namely 1) using white out space 2) using white space. In this problem we have to accept the string using white space. While accepting the string using white space we have to use a special format specifier %[^character] instead of %s. (As we know we use %s format specifier while accepting the string from user). So in the program below we use %[^\n] specifier. It means program accept every thing until new line character(\n)i.e. enter key is not encountered. So using this format specifier we accept required string with white space.
    After accepting the string we calculate the length of the string using standard function strlen(). Then we execute a for loop which runs from zeroth location to length-1 location. For every iteration we check whether character at current location is equal to a,e,i,o,u,A,E,I,O,U and if match is fount we increment the counter by one otherwise go for next iteration. In this way we found the total count of vowels in the given string.
    Please include stdio.h, conio.h and string.h header files before main function.
    #include
    #include
    #include
    void main(void)
    {
    char name[20];
    int i,count=0,len;
    clrscr();
    printf("Enter any string\n");
    scanf("%[^\n]",name);
    len=strlen(name);
    for(i=0;i {
    if((name[i]=='a'||name[i]=='e'||name[i]=='i'||name[i]=='o'||name[i]=='u'||name[i]=='A'||name[i]=='E'||name[i]=='I'||name[i]=='O'||name[i]=='U'))
    {
    count++;
    }
    }
    printf("\nEnterd string is %s",name);
    printf("\nTotal number of vowels in the given string are %d",count);
    getch();
    }


  • Sign In to post your comments