Im hoping this can be a simple fix :/

My Problem: I am trying to take a large .txt document and find and remove duplicate words within the text. However, the .txt document was a novel and it contains strings with periods and commas tagged on to the end of them. I am trying to remove those characters so I just have letters left.

Here is the function I wrote to do this:

string lowercase(string data)
{
string output;

for (int i=0; i<data.length(); i++)
{
if ( data[i] == 'a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z'||'A'||'B'||'C'||'D'||'E'||'F'||'G'||'H'||'I'||'J'||'K'||'L'||'M'||'N'||'O'||'P'||'Q'||'R'||'S'||'T'||'U'||'V'||'W'||'X'||'Y'||'Z')
{
output = output + (char) tolower(data[i]);
}

}

return output;

The program builds and runs but the strings such as ( happy.) still output "happy." It is as if the if statement never even does the comparison. I'm hoping that I am jsut missing something small. Thanks a lot!
the condition is wrong, it should be if (data[i] == 'a' || data[i] == 'b' || ... ) but you can simply use if ( isalpha(data[i]) )
Thanks a ton! That fixed it...
Instead of using all that data[i] information, you could use half that information just by taking and using the function tolower(), and where () is, use a variable to hold the letters your grabbing. so something like
tolower(letter)
isalpha works both for lower and upper case
if you were wondering, the problem was that you can't compare that way. all the letters are integers greater than one, so it's like says if (data[i] == 'a' || true...) which is always true. you would have to do if (data[i] == 'a' || data[i] == 'b' ...) or if ((data[i] >= 'a' && data[i] <= 'z') || (data[i] >= 'A' && data[i] <= 'Z'))

but you'll be left with letters without spaces or anything distinguishing the words.
Last edited on
Topic archived. No new replies allowed.