dimanche 22 février 2015

Compaing single characters of a C++ string to C string char array

I am doing an assignment for my C++ class and I am having some issues comparing a string to a char. Here is my code that is relevant:





void function(string, char[], string[], int);

// ...

fucntion(userstring, key1, key2, keysize);

// ...

void function(string userstring, char key1[], string key2[], int keysize)
{
int length = userstring.length();
for (int currentChar = 0; currentChar < length; currentChar++)
{
for (int count = 0; count < keysize; count++)
{
if (userstring[currentChar] == (key1[count] || tolower(key1[count])))
cout << key2[count] << " ";
else if (count == keysize-1)
cout << "\"" << userstring[currentChar] << "\" ";
}

}

}



if userstring is "testing" the output is "t" "e" "s" "t" "i" "n" "g" ...


I have not tried comparing strings like this before and I honestly don't even know how to debug and check what is being compared exactly. Any help would be appreciated.


Aucun commentaire:

Enregistrer un commentaire