Must you use arrays?
If you use
std::string a lot of your problems are solved for you.
Arrays must have a length
This does
not declare an array of unknown size -- not the way you are thinking it does. You must give your arrays dimension.
char A[1000] = {'\0'};
That is an array of 1000 elements, all initialized to zero.
The user will always press ENTER at the end of every input.
Never forget it. So after you get things like
choice, make sure to read the ENTER key (newline):
1 2
|
cin >> choice;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
|
Sorry it isn't prettier, but this is how you read the character
and the newline.
Do the same thing when you read
r:
1 2
|
cin >> r;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
|
Read a c-string
To read a string from the user, you can simply say:
cin.getline( A, 1000 );
(This is similar to how you would do it with a std::string, except that this input is limited to 999 characters, and you don't know if there is more...)
Get the length of
A with the C library
strlen() function:
Loops
I'm not sure why you have a nested loop going to iterate over a single string. Just use one loop.
1 2 3 4 5 6
|
for (int i=0; i<l; i++){
if (A[i]=='a'){
A[i]='Z';
}
...etc
}
|
if...else if...else
Use a switch.
There is no need to say
A[i] = A[i];
.
Printing a string
All you need to print is
cout << A << "\n";
Variable names
You are not being very careful how you name your variables. For this reason you are being confused.
l is a particularly bad name for a variable. Is it a 1 or an l? What does 'l' mean?
Choose a more descriptive name, like "length".
b In your code, you have a variable named "b", which does nothing. Don't you mean "length"?
y does nothing. Get rid of it. (It shouldn't be global either.)
r You probably named this "r" because you couldn't name it "y", right? Why not "repeat" or something like that?
choice Good name. User's choice from the menu options.
Indentation
Watch your indentation too.
Hope this helps.