C programming help please (not c++) I was given this question to write a program
ID: 3888303 • Letter: C
Question
C programming help please (not c++)
I was given this question to write a program for and I don't understand nested loops. Can someone please show me what the code is supposed to look like so I can try to better understand it?
The program reads an unknown number of words – strings that are only lowercase letters – stopping when the word quit is entered. None of your strings will ever have more than 20 characters. The program counts the number of times that each vowel is seen, and prints the total number of occurrences of each vowel at the end. Using the input: the quick brown fox jumps over the lazy old dog quit The program should generate five lines of output in the format shown below: The vowel a occurred 1 times The vowel e occurred 3 times The vowel i occurred 1 times The vowel o occurred 5 times The vowel u occurred 2 times Hint: this program uses nested loops, the outer loop reads and processes a string, the inner loop iterates over each character in the string – identifying/counting the vowels in that one string.
Explanation / Answer
please see below code for your problem.
Comments are added inline with code to make it more readable.
Do Rate and give me feedback which will allow me to keep working for you.
Thanks
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char strList[100][20];
int count[100];
int i,j;
char *ps="quit";
char str[20];
i=0;
//clear the output consol screen
clrscr();
//here we are initialising the counters for each vowel
count[0]=0;// counter for 'a'
count[1]=0;//counter for 'e'
count[2]=0;//counter for 'i'
count[3]=0;//counter for 'o'
count[4]=0;//counter for 'u'
//reading the strings
do
{
scanf("%s",str);
if(strcmp(str,ps)!=0)
{
strcpy(strList[i],str);
}
i++;
}
while(strcmp(str,ps)!=0);
for(;i>=0;i--)
{
for(j=0;j<strlen(strList[i]);j++)
{
if('a'==strList[i][j])
{
//incrementing counter for a
count[0]++;
}
else if('e'==strList[i][j])
{
//incrementing counter for e
count[1]++;
}
else if('i'==strList[i][j])
{
//incrementing counter for i
count[2]++;
}
else if('o'==strList[i][j])
{
//incrementing counter for o
count[3]++;
}
else if('u'==strList[i][j])
{
//incrementing counter for u
count[4]++;
}
}
}
// printing all counter for each vowel
printf("Vowel a occured %d times ",count[0]);
printf("Vowel e occured %d times ",count[1]);
printf("Vowel i occured %d times ",count[2]);
printf("vowel o occured %d times ",count[3]);
printf("Vowel u occured %d times ",count[4]);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.