String arrays exercise

0 Gent Ymeri · May 15, 2015
First of all I want to introduce myself.
My name is Gent and I come from Europe.
I am a first year student doing a B.Sc in BusinessInformatics (in U.S is knows as Information Systems/ Information Technology as I know)

In this semester I have started to learn programming and I have a question which I can't solve without errors, if you guys can help me solve it, I would be thankful. 

The question is about String Arrays. 
Allow user to enter one text, then: 
a) Print the entered text in reverse order. 
and b) in the reverse text replace each lower case letter with upper case and vice versa. 

Thank you in advance! 

Post a Reply


Oldest  Newest  Rating
+1 Sengngy Kouch · May 17, 2015
Hi there, nice to know you up here.

First of all, instead asking people to give you the solution, why don't you show us what you got so far?


Alright, enough said.

1. I don't know if you already know how to get input from user. so here is how:

First, make sure you have type #include<iostream> in your file.
string input;
cout  input; // this allow you to get input from user.

Remember that this only allow user to input a single string. Without any white space. 

2.Print the entered text in reversed order.

First of all, I think you should know how to use For loop, don't  you?
and do you know how to use substr ?

The main idea is to go through half way of the  "for loop"  and swap the right character with left character.

Example.   your string is "ABCDE"

so you need loop through the string half way by:

for ( int i = 0; i< input.length() / 2; i++)
  // do the swap here.

swapping is easy when you master it.

string temp;
temp = leftSubstring;
leftSubstring = rightSubString; // note: this is just a pseudo code, you need to use substr here.
rightSubString = temp;

3.Replacing the case of the letter is easy.

you need to type :  #include <algorithm> 

then use thing line of code:

transform(input.begin(), input.end(), input.begin(), ::tolower); // from upper to lower.

transform(input.begin(), input.end(), input.begin(), ::toupper); // from lower to upper.

I hope this help.
+1 Sengngy Kouch · May 17, 2015
I forget that this site won't allow me to use << in the code segment,

so here it for #1.

string input;

cout << "Please input a string" << endl;
cin >> input; // this allow you to get input from user.
+1 Gent Ymeri · May 18, 2015
Thank you! 
Even though I already solved the problem.

Also, you are right, I should have written the code. 

The problem was that I didn't wrote the correct line of code for reversing the string, I knew the rest of the code how should it be done. 
it was  _strrev(text) which I didn't write for some reason during the lectures.
Thank you for explaining me the things you did above. I also can see that I gave a wrong impression.. but, still thanks for taking your time and consideration to help me. 
the code you wrote about swapping, looks to me like kind bubble sort thing!? 

here is the code I wrote for the solution. Any feedback, would be appreciated. 

#include <iostream>
using namespace std;
int main(){
char text[100];
cout << "Enter text:";
cin.getline(text, 100);
cout << "The text in reverse order is:" << text<<endl;
int i, n;
n = strlen(text);
for (i = 0; i < n; i++){
if (islower(text))
text = toupper(text);
else if (isupper(text))
text = tolower(text);

for (i = 0; i < n; i++)
cout << text;
cin.get(); cin.get();
return 0;

this is the way my prof. explained how to deal with string arrays during lectures. 
your method seems a bit different, I think I will try to implement it also.

Thanks one more time!

+1 Sengngy Kouch · May 18, 2015
No problem man! 

And the way you your program work is different from mines due to the fact that I used "String" and you use Char.  Both work great!

Happy Coding!
+1 Sengngy Kouch · May 18, 2015
Oh, by the way, the swapping that I use is not a bubble sort method.

The way my swapping method is:

If you have ABCDE.

you go to the first index, which is A.  Now swap the A with E.

now you have EBCDA.

Then the second index. which is B, now swap it. and you have EDCBA.

Because you only have to loop through only half size of the string, which is the length of the string  = 5. and half of it is 5/2 = 2.
We got two because integer doesn't get the decimal point. 

I dont know if this come across, but yeah. lol, Happy Coding!! :D
+2 Michael Bradford · May 19, 2015
Yeah just to reiterate since your learning what you have isn't really an array of strings so much as an array of char's. Strings are Chars (like 'M') strung together. Your professor called it a "string Array" which I find could be confusing to a new student.. It is an array of chars which is also known as a String.
0 Sengngy Kouch · May 19, 2015
@Michael : good advice
0 Gent Ymeri · May 20, 2015
Sengngy Kouch I see, that looks something similar to binary search.? 

Michael and Sengngy, thanks! 
0 Sengngy Kouch · May 20, 2015
Any time man!
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator