Sunday, July 12, 2009

C++ programming?

1. The user's string all up-cased (converted to ALL CAPITALS).


2. The user's string all down-cased (converted to all lower case)


3. Report the number of vowels in the input string ('a', 'e', 'i, 'o', 'u' and uppercase equivalents)).


4. Report the number of consonants in the input string (count 'y' as a consonant).


5. Display the string in reverse order ("Hi, Mom" would be "moM ,iH").

C++ programming?
#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





string toUpper(string input);


string toLower(string input);


bool isVowel(char someChar);


string reverseString(string input);





int main()


{


string userString;


int vowels = 0;


int consonants = 0;





cout %26lt;%26lt; "Enter a string: ";


getline(cin, userString);





for(int i = 0; i %26lt; userString.length(); i++)


{


if(isalpha(userString[i]))


{


if(isVowel(userString[i]))


vowels++;


else


consonants++;


}


}





cout %26lt;%26lt; endl %26lt;%26lt; endl;


cout %26lt;%26lt; "Upper case: " %26lt;%26lt; toUpper(userString) %26lt;%26lt; endl;


cout %26lt;%26lt; "Lower case: " %26lt;%26lt; toLower(userString) %26lt;%26lt; endl;


cout %26lt;%26lt; "Reversed: " %26lt;%26lt; reverseString(userString) %26lt;%26lt; endl %26lt;%26lt; endl;


cout %26lt;%26lt; "Vowels: " %26lt;%26lt; vowels %26lt;%26lt; endl;


cout %26lt;%26lt; "Consonants: " %26lt;%26lt; consonants %26lt;%26lt; endl;





cin.get();





return 0;


}





string toUpper(string input)


{


for(int i = 0; i %26lt; input.length(); i++)


{


input[i] = toupper(input[i]);


}


return input;


}





string toLower(string input)


{


for(int i = 0; i %26lt; input.length(); i++)


{


input[i] = tolower(input[i]);


}


return input;


}





bool isVowel(char someChar)


{


someChar = tolower(someChar);





if(someChar == 'a' || someChar == 'e' || someChar == 'i' || someChar == 'o' || someChar == 'u')


return true;


else


return false;


}





string reverseString(string input)


{


string reversed;





for(int i = input.length() - 1; i %26gt;= 0; i--)


{


reversed += input[i];


}


return reversed;


}


No comments:

Post a Comment