Hi, I am beginner in C++ and I'm trying to figure out how to create a program to check if different words in a strings has the exact same alphabetic characters and find their location.
(Ignoring spaces and any non-alphabetic character)
Also, it doesn't matter if the char is lower or upper as long as it's the same char.
I have updated the code in the first post using ShadowCODE's first approach.
However there's an error whenever I put a space or a non-alpabetic into one of the strings to compare..
(note that the main is in a separate file)
Ok. made the following changes to your code.
First , the prototype in line 5 bool HaveSameChar(constchar * const s1, int n1, constchar * const s2, int n2)
Its supposed to be bool HaveSameChar(constchar * s1, int n1, constchar *s2, int n2)
If you keep s1 and s2 as const, then you cant increment them as you did in lines 14 and 18.
Also in the for loops at line 21 and 28, use temp.length() and temp2.length() respectively since we are now dealing with temp and temp2 instead of s1 and s2.
Also in the body of those loops, you should decrement i after erasing.
So you should have
1 2 3 4 5
if (!isalpha(temp.at(i)))
{
temp.erase(temp.begin() + i);
i--; //this will check that position again
}//do same for temp2
I just figured those a couple minutes after posting haha. Thanks a lots! Your code helped me a lots.
I also have another question if you have time on you.
Would you know a way to find the number of same anagram (or sentence with same char) in a long string with multiple word?
ex: TheInput = " Forum, I'm a beginner ++, RumFo, begiN aER Mi, KOBO, book, MUR FO"
Result : out[0]= 0, 4, 7 (location of the 3 word with "forum" chars)
out[1]= 2, 3 ( 2 words with "imabeginner" chars)
out [2] = 5,6 ( 2 words with "book" chars)