Note: Vowels (a,e,i,o,u) can be in upper or lower case to count. You can have un
ID: 3758407 • Letter: N
Question
Note: Vowels (a,e,i,o,u) can be in upper or lower case to count.
You can have unlimited number of sentences, the last sentence has nothing in it but a "return" character in it. When this empty sentence is entered, you end the program. Keystrokes counter does NOT include the enter/return key. You must use the library routines in ctype.h and the SWITCH statement and the toupper or tolower and ISALPHA functions. Output will be for EACH sentence.
SAMPLE INPUT:
This is a Number 1 SAMPLE INPUT SentANce out of 1.
Sample Sentence#2
Sample Sentence#3
...
empty sentence denoted by nothing but 'return' key
SAMPLE OUTPUT, for EACH sentence above:
YourName Program#6 CSCI1110
Keystrokes: 9999
Alpha Characters: 9999
Vowels: 9999
----PARTIAL SAMPLE OUTPUT:
Keystrokes: 50
Alpha Characters: 37
Vowels: 15
Here is my code but the output only comes up with 1 keystroke and no vowels/alpha characters no matter the sentence. I also don't know how to incorporate the switch statement and the toupper/tolower any help would be greatly appreciated :
/* This program will be given an input sentence and analyze it to give an output of the
number of keystrokes, alpha characters and vowels.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int keystrokes = 0; //Initial number of keystrokes.
int letters = 0; //Initial number of total letters.
int vowels = 0; //Initial number of vowels.
int i = 0;
char sentence[9999]; //Chararacter array specified to user's input.
printf("Ryan J. Fleming Program 6 CSCI1110 ");
while((sentence[i] = getchar()) != ' ');
{
if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] ==
'o' ||sentence[i] == 'u')
{
++keystrokes;
++letters;
++vowels;
}
else
{
if (isalpha(sentence[i]) == 1) //if character is true
{
++keystrokes;
++letters;
}
else
{
++keystrokes;
}
}
i++;
}
printf("Keystrokes %d ", keystrokes); //Output number of keystrokes in given sentences
printf("Alpha Characters %d ", letters); //Output number of uppercase characters in given sentences
printf("Vowels %d ", vowels); //Output number of vowels in given sentences
return 0;
}
Explanation / Answer
Remove ; at the end of while loop.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int keystrokes = 0; //Initial number of keystrokes.
int letters = 0; //Initial number of total letters.
int vowels = 0; //Initial number of vowels.
int i = 0;
char sentence[9999]; //Chararacter array specified to user's input.
printf("Ryan J. Fleming Program 6 CSCI1110 ");
while((sentence[i] = getchar()) != ' ')
{
if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] ==
'o' ||sentence[i] == 'u'|| sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] == 'I' || sentence[i] ==
'O' ||sentence[i] == 'U')
{
++keystrokes;
++letters;
++vowels;
}
else
{
if (isupper(sentence[i])) //if character is true
{
++keystrokes;
++letters;
}
else
{
++keystrokes;
}
}
i++;
}
printf("Keystrokes %d ", keystrokes); //Output number of keystrokes in given sentences
printf("Alpha Characters %d ", letters); //Output number of uppercase characters in given sentences
printf("Vowels %d ", vowels); //Output number of vowels in given sentences
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.