Is this an example of c or c++

I found references to a c programming problem nearly identical to this one and cannot seem to find anything even remotely close to this in c++
Write a program that prompts the user to input a string. Your program should convert the
message to upper-case letters, substitute digits for certain letters (A->4, B->8, E->3, I->1, O->0,
S->5), and the append 10 or so exclamation marks.
Hint: Store the original message in char array, and then go back through the array, translating
and printing characters one by one.

I really am looking for answers as to what this is saying (A->4, B->8, E->3, I->1, O->0,S->5)

I was going to use toupper to accomplish this but am thinking that would be incorrect. I've no code as of yet and am just looking for some kind guidance. thanks in advance

convert the message to upper-case letters
toupper()
substitute digits for certain letters
Many approaches could be used here. Most common one with iteration through string and replacing letters using chained ifs or a switch works both in C++ and C. As do several other.
append 10 or so exclamation marks.
In C++ using std::string it is done in one simple line: str += "!!!!!!!!!!";
Thank you very much. I had a hand to forehead moment just now when looking at the part that reads replace A->4 etc. and I realized it was replacing letters with numbers that sort of resemble the letters. Thanks again. I'll post some code soon to show your help. again Thanks.
Last edited on
I got this much but haven't yet figured out how to replace the letters with digits (A->4, B->8, E->3, I->1, O->0,S->5) and when it prints out all of the blank spaces are missing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
    int i;
    char a;
    char sentence[100];
    cout << "Enter a string of characters to convert to uppercase and replace some characters with digits: ";
    
    for (i = 0; a < 100; i++)
    {
    cin >> sentence[a];
    cout << static_cast<char>(toupper(sentence[a]));
    }  
    system("pause");
    return 0;    
}

I fixed the no blank spaces issue and am now working on replacing the letters with digits. here is my code PLEASE please please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int i;
    char a;
    char sentence[100];
    cout << "Enter a string of characters to convert to uppercase and replace some characters with digits: ";
    
    for (i = 0; a < 100; i++)
    {
    
    cin.get(sentence[a]);
    cout << static_cast<char>(toupper(sentence[a]));
        
    } 

    system("pause");
    return 0;    
}

You should create a std::string instead of using a character array, then read the entire line of input with std::getline.
I would consider that but the assignment calls for me to " Store the original message in char array, and then go back through the array, translating
and printing characters one by one." this is referring to changing the letters to digits.
If you're not willing to risk your grade, I understand.

The main problem with your code right now is that your loop is wrong. You should be using i where you are using a - in fact you don't need a for anything at all.
I think this is it but for this Changing (A->4, B->8, E->3, I->1, O->0,S->5) and appending about ten or so exclamation marks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;


int main()
{
    int i;
    char a;
    char sentence[100];
    cout << "Enter a string of characters to convert to uppercase and replace some characters with digits: ";
    
    for (i = 0; i < 100; i++)
    {
    
    cin.get(sentence[a]);
    cout << static_cast<char>(toupper(sentence[a]));
 
    } 

    system("pause");
    return 0;    
}
I will repeat: you do not need the variable a for anything at all.

If your assignment did not require the use of a character array, you could do without it too. However, the assignment requires that you read in all the input first and only after that you may modify it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cctype>

char translate( char c )
{
    c = std::toupper(c) ; // if lower case, convert to uppercase

    // substitute digits for certain letters
    switch(c)
    {
        case 'A' : return '4' ;
        case 'B' : return '8' ;
        case 'E' : return '3' ;
        case 'I' : return '1' ;
        case 'O' : return '0' ;
        case 'S' : return '5' ;
        default: return c ;
    }
}

int main()
{
    const int SZ = 1024 ;
    char cstr[SZ] ;

    std::cout << "phrase? " ;
    std::cin.getline( cstr, SZ ) ; // read a whole line, up to max SZ-1 characters
    // http://www.cplusplus.com/reference/istream/istream/getline/

    for( int i = 0 ; cstr[i] != 0 ; ++i ) // for each char, up to, not including the terminating null char
        std::cout << translate( cstr[i] ) ;

    std::cout << "!!!!!!!!!!\n" ; // append 10 or so exclamation marks
}
Topic archived. No new replies allowed.