I've read around and made this code to loop through my two strings and add up all of the ascii values of the characters in them. However, it's not working...it's not even iterating. When I look at the cout for h and hi, I get 112 & 205976. Shouldn't it be two lines 104 and 209?

I do a lower case conversion on both strings just before I do this code, but I don't see how that would cause me any problems.

I'm trying to compare two strings to see if they are anagrams or not. If I make both lower case so if they input them in upper case it wouldn't matter, then convert them both to ascii values and then compare those values to each other, if they are both the same ascii values they will be anagrams.

Thanks.

for (c=0;c < phrase1.length();c++)
    {
        asciiphrase1 = asciiphrase1 + int(phrase1[c]);    
        c++;
        cout<<asciiphrase1<<endl;
    } 

    
       for (d=0;d < phrase2.length();d++)
    {
        asciiphrase2 = asciiphrase2 + int(phrase2[d]);    
        d++;
        cout<<asciiphrase2<<endl;
    }

I didn't for the asciiphrases, but it's still not working.

For test and will I get this:
116
231
119
227

I was hoping to see 448 & 440 at the end.

DUh....lol

Thanks a lot.

The original code was surrounded in a while statement, but in my case that crashed the program and gave me an infinite loop so I had to change it to a for loop and change some of the syntax, but I forgot to take out the increments.

For some reason, it's adding the value of the last character twice. I'll just make a code mod to subtract that.

Does this code make sense?

asciiphrase1 = asciiphrase1 - (int(phrase1[c]);  
asciiphrase2 = asciiphrase2 - (int(phrase2[d]);  

The last value of c and d would be whatever length the string was. I'm just trying to delete the ascii value of the last character that's being added twice. It doesn't seem to be subtracting it. I placed this just after the for loop for the ascii converter, but not inside it.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Got it, I just subtracted one from string.length() in each of the for loops...now the totals are coming out perfect.

Is there a library function for strings that strips all of the punctuation??? I haven't been able to find one.

This article has been dead for over six months. Start a new discussion instead.