954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ String Manipulation

Hey DaniWeb, I'm having a tough time with this piece of code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
            if(e[i] == "{")
            {
                    e[i] = "0";
            }
            if(e[i] == ":")
            {
                    e[i] = "a"
       }
       return e;
}

It should "encrypt" the string, using only alpha-numeric characters, as in z+1 = {, so z is converted to 0, and 9+1 = :, so 9 is converted to a. The error I get is 49 C:\Dev-Cpp\files\ver1\main.cpp: ISO C++ forbids comparison between pointer and integer
Can anybody help me with this?

Thanks!

dylank
Junior Poster in Training
66 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

Hey buddy !!!
the problem with ur code is that while comparing in the if condition,
if(e[i]=="{")...
you are using double " " marks.
here, if e is a string, then any index of that string will be a character.
for example if e="hello", then e[4]='o'.

so while comparing you should use only a single ' mark in the if condition because what you are actually doing is comparing a character only.

so your code should be like this:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i )
       {
            ++e[i];
            if(e[i] == '{')
            {
                    e[i] = '0';
            }
            if(e[i] == ':')
            {
                    e[i] = 'a';
            }
       }
       return e;
}


hope this is clear :)Hey DaniWeb, I'm having a tough time with this piece of code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
            if(e[i] == "{")
            {
                    e[i] = "0";
            }
            if(e[i] == ":")
            {
                    e[i] = "a"
       }
       return e;
}

It should "encrypt" the string, using only alpha-numeric characters, as in z+1 = {, so z is converted to 0, and 9+1 = :, so 9 is converted to a. The error I get is 49 C:\Dev-Cpp\files\ver1\main.cpp: ISO C++ forbids comparison between pointer and integer
Can anybody help me with this?

Thanks!

ayan2587
Junior Poster in Training
60 posts since Jul 2009
Reputation Points: 8
Solved Threads: 0
 

Great, the code worked fine! although, I cannot convert a 9 into an 'a'. That is, when I enter 9, I get 0. How can I fix this?

dylank
Junior Poster in Training
66 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

Well dude..if u run the above code i have pasted..it definitely converts 9 into 'a';
if not give me a test case where this does not happen..i shall take a look and get bak to u..

Great, the code worked fine! although, I cannot convert a 9 into an 'a'. That is, when I enter 9, I get 0. How can I fix this?
ayan2587
Junior Poster in Training
60 posts since Jul 2009
Reputation Points: 8
Solved Threads: 0
 

Here is where it does not work for me:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
            switch(e[i]) {
                 case 0:
                      e[i] = 'a';
                 case '{': 
                      e[i] = '0';
                 case ':': 
                      e[i] = 'a';
                 case '[': 
                      e[i] = '0';
                 default: e[i] = e[i];
            }
       }
       return e;
}


If you need, I can post the entire code for you, it is for a KeygenMe program.

dylank
Junior Poster in Training
66 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

Oh yea, Ive tried copying your code directly, it works now.

I guess maybe I replaced a charachtar or something? Heh..

Thanks anyway!

dylank
Junior Poster in Training
66 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

Dude there seems to be a fundamental flaw in ur code.
the switch-case statements that u have written here are quite wrong.
In the switch statements, we often use a break; statement, so that the flow of the program for a particular case stops when we find the desired value.

Here, in ur program, what is actually happening is that the cases continue to be checked even after finding the desired value.
That is the reason u are unable to get 'a' when u key 9 into the program.

So here's the correct code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
 

            switch(e[i]) {
                 case 0:
                      e[i] = 'a';
                      break;
                      
                 case '{': 
                      e[i] = '0';
                      break;
                      
                 case ':':
                      e[i] = 'a';
                      break;
                      
                 case '[': 
                      e[i] = '0';
                      break;
                      
                 default: e[i] = e[i];
            }

       }
       return e;
}

Ur code should run fine nw... :)Oh yea, Ive tried copying your code directly, it works now.

I guess maybe I replaced a charachtar or something? Heh..

Thanks anyway!

ayan2587
Junior Poster in Training
60 posts since Jul 2009
Reputation Points: 8
Solved Threads: 0
 

Dude there seems to be a fundamental flaw in ur code. the switch-case statements that u have written here are quite wrong. In the switch statements, we often use a break; statement, so that the flow of the program for a particular case stops when we find the desired value.

Here, in ur program, what is actually happening is that the cases continue to be checked even after finding the desired value. That is the reason u are unable to get 'a' when u key 9 into the program.

So here's the correct code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
 

            switch(e[i]) {
                 case 0:
                      e[i] = 'a';
                      break;
                      
                 case '{': 
                      e[i] = '0';
                      break;
                      
                 case ':':
                      e[i] = 'a';
                      break;
                      
                 case '[': 
                      e[i] = '0';
                      break;
                      
                 default: e[i] = e[i];
            }

       }
       return e;
}

Ur code should run fine nw... :)


i want to ask a question that confuse me. what is " ++e[i];" for

compeng
Newbie Poster
2 posts since Feb 2010
Reputation Points: 9
Solved Threads: 0
 

thats just program specific....
dont bother about that... that was not the problem here...
its just a part of program !! :)

i want to ask a question that confuse me. what is " ++e[i];" for
ayan2587
Junior Poster in Training
60 posts since Jul 2009
Reputation Points: 8
Solved Threads: 0
 

Hey buddy !!! the problem with ur code is that while comparing in the if condition, if(e[i]=="{")... you are using double " " marks. here, if e is a string, then any index of that string will be a character. for example if e="hello", then e[4]='o'.

so while comparing you should use only a single ' mark in the if condition because what you are actually doing is comparing a character only.

so your code should be like this:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i )
       {
            ++e[i];
            if(e[i] == '{')
            {
                    e[i] = '0';
            }
            if(e[i] == ':')
            {
                    e[i] = 'a';
            }
       }
       return e;
}

hope this is clear :)

i want to ask a question that confuse me. what is " ++e[i];" for


That is too add one to the character, so if the char was a, it would now be b.

dylank
Junior Poster in Training
66 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: