Separating characters from a string without using array. Is it possible?

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! ^_^
A possible solution for for counting the vovels:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string sentence;
std::getline(std::cin, sentence)
for( char letter : sentence)
{
    switch(letter)
    {
        case 'a': ++a; break;
        case 'e': ++e; break;
       ...


std::cout << "a = "    << a
          << "\ne = " << e
          << "\ni = " << i
          << "\no = " << o
          << "\nu = " << u << '\n';
Last edited on
For assigning a telephone number to a name I suggest using a struct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Record
{
    std::string telNr;
    std::string name;
};

void read_record( Record & record)
{
    std::string tmp;

    std::cout << "Enter a telephone number:";
    std::getline( std::cin, tmp);
    record.telNr = tmp;

    std::cout << "Enter a name:"
    std::getline( std::cin, tmp);
    record.name = tmp;
}


Printing the record by not using more then 7 letters for the name:
1
2
3
4
5
6
7
8
9
10
void print_record( const Record & record)
{
    std::cout << "name: ";
    for( std::size_t i = 0; i < 7; ++i)
    {
         std::cout << record.name[i];
    }
    std::cout << "\nTelNr: ";
    std::cout << record.TelNr << '\n';
}
Last edited on
I forgot show how to output the hyphen after the 3rd digit, here it is:

1
2
3
4
5
for( std::size_t i = 0;  i < record.telNr.length();  ++i)
{
    std::cout << record.telNr[i];
    if( i == 2) std::cout << '-';
}

Last edited on
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.
}
Last edited on
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);
}
Last edited on
nuderobmonkey wrote:
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>
using namespace 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';
   }
}
1	1
2	4
3	9
4	16
5	25
6	36
7	49
8	64
9	81
10	100


Last edited on
@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.
not sure but try c++17 (not sure why you would use anything less than 17 at this point, it seems to be pretty well supported).
Last edited on
Try again, please, @nuderobmonkey, the closing brace vanished when I tried the two-column format. It should work from c++11, I believe.

Note to self: it's really hard to edit on an iPad.
Last edited on
OOps, I'd made a mistake at setting my compiler flags. You're right man, your code works also for -std=c++11 :)
Topic archived. No new replies allowed.