Ok, so here are the questions I am faced with,
First Question,
Write a program that will ask user to enter a sentence. Count the number of each vowel in it using switch statement. For example, if user enter “Hello Friends” then it will print the following result:
a = 0
e = 2
i = 1
o = 1
u = 0
Second Question,
To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters. Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than seven letters, then process only the first seven letters. Also output the – (hyphen) after the third digit. Allow the user to use both uppercase and lowercase letters as well as spaces between words. Moreover, your program should process as many telephone numbers as the user wants.
Now, from what I understand, we are supposed to take a string in both these programs and then compare all the characters of the string one by one. But I do not know how can I do it. I tried to think of different logics but none worked for me so I tried to find a solution on internet. However, I could only found solutions which used arrays... My problem with arrays is this that first, I don't know even a thing about arrays... Second, since we haven't yet studied arrays, so we can't use arrays in these programs even if we know how to use them.
So, can anybody tell me of a solution by which I can separate characters from a string using maybe for/while loop? or any other way by which these questions can be solved without using arrays?
Thanks in advance! ^_^
Hey, thanks a lot for your reply!
Your solution to the vowel counting looks great, thank you for sharing it :)
However, if possible, could you explain it a little bit :P
I am still in the very beginning of C++ and we haven't learnt a lot of commands yet... and hence, I am finding it a little difficult to understand the for loop you used there :/
For what I know, the for loop structure involves initialization (which you did for char letter), and then comes a condition separated by a semi colon and at the end, an increment condition which is again separated by semi colon... I learnt that if we only put the middle condition in for loop then it will also work perfectly as long as you put semi colon behind and ahead of it as well (meaning that the structure of for loop must remain intact for it to work)
But you didn't use any semi colon... and you also used a colon there, which I don't know why you used. So, is it possible for you to explain it a little bit or maybe share a video link of some youtube video which explains such thing?
As for the second solution, first thanks for it... Here also you used a struct command which probably is some data structure, one of many things which again we haven't yet learned... But I guess it would be quite easy to make the second program using the switch function, by simply re-using the first solution.
Hello redfury, the for with the colon is a so called foreach-loop. E.g, you could read for( char letter : sentence) {...}
as: For each char within 'sentence' - which we call 'letter' - do ...
Be aware that each 'letter' char would be a copy from the 'sentence' string, therefore you can't change with that the chars within 'sentence'. If you would do that, you would have to use the passing by 'reference' to that (by adding an & beween type name and var name).
1 2 3 4 5
for( char & letter : sentence)
{
letter = std::toupper(letter) // This would change each 'letter' inside 'sentence'
// because 'letter' refers to the chars of the 'sentence' string.
}
Ok I get it. Studied more about foreach loop and found that it is basically to check arrays.. but could be used here brilliantly as well...
It makes all sense now, thank you very much for your help, greatly appreciate it! :)
You cannot traverse plain arrays with a foreach (range based for loop), because it works only on container classes like std::vector, std::string and such. That's because such containers provide an so called iterator, which a plain array doesn't have. The foreach is syntactic sugar for simplifying such things like:
1 2 3 4
for( std::string::iterator it = sentence.begin(); it != sentence.end(); ++it)
{
*it = std::toupper(*it);
}
You cannot traverse plain arrays with a foreach (range based for loop), because it works only on container classes like std::vector, std::string and such.
Not quite true ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
int A[] = { 1, 2, 3, 4, 5 };
for ( int i : A )
{
cout << i << '\t' << i * i << '\n';
}
for ( int i : { 6, 7, 8, 9, 10 } )
{
cout << i << '\t' << i * i << '\n';
}
}
@lastchance
That this works is new for me. Since which C++ standard it was introduced? I tested your code with --std=c++11 and there it was still not possible.