Jump to content

so for an exam which i have an hour left for, I need to write a vowel counter where the user inputs a c string, can anyone help me ? Here is the question: 

Write a function that takes a C string as an input and counts the number of vowels in the C string. Vowels must include both upper and lower case a, e, i, o, u, and y. You are not allowed to use any other functions to do this.  Do not write a main function. Your function does not do any cin or cout. Remember, C strings are terminated with the '\0' character. Make sure to properly format all your code.


LIKE an idiot, i did it without the c string and i'm having trouble switching the formatting. Can anyone help? Here is my current program. 

 

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <iomanip>
 
using namespace std;
int main() 
{
 
char sentence = ' ';
int count = 0 ;
 
cout << "Hello! Enter a sentence and I will count the number of vowels!";
cin.get(sentence);
 
cout << "VOWELS: " << endl;
 
while (sentence != '\n')
{
cin.get(sentence);
switch (toupper(sentence))
{
case 'A' : cout << sentence << ", ";
  count++;
break;
case 'E' : cout << sentence << ", ";
  count++;
break;
case 'I' : cout << sentence << ", ";
  count++;
break;
case 'O' : cout << sentence << ", ";
  count++;
break;
case 'U' : cout << sentence << ", ";
  count++;
break;
case 'Y' : cout << sentence << ", ";
count++;
break;
}
}
cout << endl << "Your sentence had a total of" << count << " vowels!" << endl ; 
system ("PAUSE");
return 0;
}
 
Link to comment
https://linustechtips.com/topic/318202-c-string-vowel-counter/
Share on other sites

Link to post
Share on other sites

it is said to not write main function, you need create new one that takes c-string as argument, you also need look for \0, not \n, and it is probably better to use if instead of case or you can make one increment for all cases with one break.

 

your function declaration/definition would look like

int countVowels(const char *s){//Your code here, use for loop as it has iterator build in for iterating through every string character, until iterator is < strlen or s[iterator] != \0, your trick with uppercaseing letters is good.}

What I mean with using if or group cases is

char c = toupper(s[i]);//IF:if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'){    count++;}//SWITCH:switch(c){    case 'A':    case 'E':    case 'I':    case 'O':    case 'U':        count++;    break;}
Link to comment
https://linustechtips.com/topic/318202-c-string-vowel-counter/#findComment-4325341
Share on other sites

Link to post
Share on other sites

I've read your tasks again and you're probably not allowed to use toupper() function. You need convert character to upper case by yourself.

#include <iostream> // You don't need to include this library as it's only needed for our example case#include <string.h>int countVowels(const char *s){	int count = 0;	char c;		for(int i = 0; i < strlen(s); i++){		c = (s[i] >= 'a' && s[i] <= 'z')? 'A' + (s[i] - 'a') : s[i]; // It will convert character to uppercase if character is lowercase character from a to z, it does what toupper function do		if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'){			count ++;		}	}		return count;}/*	There is our example case, you shoudln't include this into your solution as your task is only to make new function that will count voweles	without including main function, so countVowels function is all you need.*/int main(){	const char *s = "Example c-string.";	std::cout << countVowels(s); // It will display 4, as only E, a, e and i are vowels.		return 0;}
Link to comment
https://linustechtips.com/topic/318202-c-string-vowel-counter/#findComment-4325522
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×