You are here:

C++/String Function


I was asked to do this program using functions from the string.h file.  Any suggestions?  
Make a small program that first displays a line that displays only vowels, then displays a line that displays only consonants from a pre-assigned string.  You may use strlen(), and strchr().  Your program should contain three strings.  

Str1 = "a sentence with vowels"
Str2 = "aeiou"
str3 = "sntnc wth vwls"

Thank you

This is a bit of a tricky question, and has a long and involved answer. I'm going to just give you some pointers so you have a guide as to what to do though. The first step is to get the length of str1 (the normal sentence) and str2 (vowles) and store their length in their own variables (of type 'int'). This is where it gets a bit confusing. Now you have to have a 'for' loop within another 'for' loop. The outer 'for' loop is used to keep track of which element you're at in str1. The inner 'for' loop is used to keep track of which element you're at in str2.

You have to compare each character of str1 against each character in str2, and this is what the loops are for. You take one character in str1, then compare it against each character in str2 to see if there are any matches (i.e. if [where i = 0,1,2,3 etc.] matches [where x = 0,1,2,3 etc.]). If there is a match (i.e. the sentence has a vowel in it), then you move onto the next character in str1. However, if the program does not find a match (i.e. the character in question is not a vowel), then you would take that character and push it onto the end of str3 (the end result of filtering out all the vowels).

I hope this helps to put you on the right path. If you have any questions about my explanation, please let me know.

Best Of Luck,


All Answers

Answers by Expert:

Ask Experts


Alan F.


Having problems with any basic or intermediate parts of C++? I can answer those questions for you using simple English. I have been taking C++ in school for a couple years, so I still remember what it was like to be a beginner (especially with C++, which is a difficult language)!


I have a C++ web page that discuses the basics of C++. The address is:

©2017 All rights reserved.