Problems With Reversing A String


Recommended Posts

ok I'm having a problem with the following..

the varable CheckHolder is not putting anything out..

it should be a backwords output of LineToCheck and I put some Cout lines to see what it is during processing and it seams to work untill the last line I get blank for CheckHolder

#include<iostream>
#include <cctype>
#include <string>

using namespace std;

//function prototypes
//void clear_screen();
//void check(const char);

//main function
int main()
{
       //local varables
       char LineToCheck[255];
       char CheckHolder[255];
       int SizeOfChar=0, y = 0;
       string str;


       cout << "Enter string:" << endl;


       cin.get(LineToCheck,255);

               str = LineToCheck;
       SizeOfChar = str.length();
       cout << "you enterd " << SizeOfChar << " charaters" << endl;

       for (int x = SizeOfChar; x >= 0; x--)
       {
               CheckHolder[y] = LineToCheck[x];
               cout << "checkholder = " << CheckHolder[y] << endl;
               y++;

       }

 

       cout << " Line to check = " << LineToCheck<< endl;
       cout << "check holder = " << CheckHolder  << endl;
       return 0;
}

my output

bash-2.05b$ g++ extra.cpp
bash-2.05b$ ./a.out
Enter string:
This is a string
you enterd 16 charaters
checkholder =
checkholder = g
checkholder = n
checkholder = i
checkholder = r
checkholder = t
checkholder = s
checkholder =
checkholder = a
checkholder =
checkholder = s
checkholder = i
checkholder =
checkholder = s
checkholder = i
checkholder = h
checkholder = T
Line to check = This is a string
check holder =
bash-2.05b$                  

Link to post
Share on other sites

to add I can see the output of CheckHolder if I add a for loop

for (int x = 0; x <=  SizeOfChar; x++)
cout << CheckHolder[x];
cout << endl;

this is not the a good soultion as I must newt strip out any spaces and puntuation, then make everything lowercase for comparison..

I don't need these answers .. as I need to learn to do them and I can only learn with trial and error.. but this char problem is makeing me mad..

(do I need to read it into a string?)

:blink:

Link to post
Share on other sites

The problem is that you're copying the entire string into CheckHolder reversed, including the terminating null character. The null char is inserted at CheckHolder[0], so CheckHolders ends up being a C string of length 0. You need to start copying from the last non-null character and then insert a null at the end of the reversed string.

It is a good idea to the use the string class whenever possible. C strings are a legacy feature.

Excursus: It's probably beyond the scope of your assignment, but C++ makes this fairly easy if you use more of the features of the standard library:

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string in;

   cin >> in;
   cout << string(in.rbegin(), in.rend()) << endl;

   return 0;
}

Edited by jcl
Link to post
Share on other sites

"One Off" errors are very easy to fall into, especially when learning C.

A 16 character string for instance is typically looped with:

For (x=0;x<16;x++).

Beginners typically forget that C arrays are indexed starting at zero, so the scan goes to length - 1 (using the "<" test instead of "<=" is a common way to cover that).

And as JCL said, it's very easy to be caught by the terminating zero byte.

Link to post
Share on other sites
The problem is that you're copying the entire string into CheckHolder reversed, including the terminating null character. The null char is inserted at CheckHolder[0], so CheckHolders ends up being a C string of length 0. You need to start copying from the last non-null character and then insert a null at the end of the reversed string.

It is a good idea to the use the string class whenever possible. C strings are a legacy feature.

Excursus: It's probably beyond the scope of your assignment, but C++ makes this fairly easy if you use more of the features of the standard library:

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string in;

   cin >> in;
   cout << string(in.rbegin(), in.rend()) << endl;

   return 0;
}

jcl,

that captures the first word until the first space..

my problem with that is I need to capture and entire line.. strip out spaces, puctuation and case and see it its the same forwared as it is backwards.. the only ohte rrequirment is I must use cin.get();

so now the question.. if I put the input in to a Char first, then move everything but spaces and puncuation into the string I should be half way there right?

I am assuming this is putting out the string twice.. once forward (rbegin) and agian (rend)

I'll go look up the string class so I understand more..

thanks

Link to post
Share on other sites
that captures the first word until the first space..

Yeah. It was just supposed to incite interest in the standard library.

so now the question.. if I put the input in to a Char first, then move everything but spaces and puncuation into the string I should be half way there right?

Right. You can combine the editing and reversal in one loop if you wish.

Edited by jcl
Link to post
Share on other sites
Yeah. It was just supposed to incite interest in the standard library

it did :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...