I have the following code in C :
string StringTest = "Test";
bool OriginalWord = true;
for (unsigned int i = 0; i < StringTest(); i ) {
string Character = towlower(StringTest[i]);
string CharacterOriginal = StringTest[i];
if (Character != CharacterOriginal) {
//t vs. T should trigger false, but never happens?
//e vs. e should trigger true
//s vs. s should trigger true
//t vs. t should trigger true
OriginalWord = false;
}
}
Note that I use towlower
instead of tolower
.
This always results in a OriginalWord=true
situation. However, e.g. Test should give me back OriginalWord=false
. Because towlower(T)
will result in t and the CharacterOriginal = T
, which means they are not the same and thus OriginalWord=false
?
What am I doing wrong? I guess it has to do something with the towlower
function
CodePudding user response:
You have some trouble with type conversions, which wouldn't fly with stricter compiler flags. Please see the comments in the code fragment below:
string StringTest = "Test";
bool OriginalWord = true;
for (unsigned int i = 0; i < StringTest.size(); i )
// do you mean StringTest.size() instead of StringTest()?
{
wint_t Character = towlower(StringTest[i]);
wint_t CharacterOriginal = StringTest[i];
// can't convert a char to a string (or const char *)
// did you meant to use wint_t instead of string?
if (Character != CharacterOriginal)
{
//t vs. T should trigger false, but never happens?
//e vs. e should trigger true
//s vs. s should trigger true
//t vs. t should trigger true
OriginalWord = false;
}
}
CodePudding user response:
Okay, there some troubles in your code, first:
string StringTest = "Test"; // you created StringTest as a single variable
//in the next steps you use it all the time as a array, lets change that.
int count = 10;
string StringTest[count] = { "Test", "test","Test", "test","Test", "test","Test", "test","Test", "test" };
for (unsigned int i = 0; i < StringTest(); i ) // you must use the size of string so:
for (unsigned int i = 0; i < count; i )
CodePudding user response:
string Character false usage, it must be a char Because tolower returns int(char) not a string. Also to get length of string -> StringTest.size();
The edited code is here!
string StringTest = "Test";
bool OriginalWord = true;
for (unsigned int i = 0; i < StringTest.size(); i ) {
char Character = tolower(StringTest[i]);
char CharacterOriginal = StringTest[i];
if (Character != CharacterOriginal) {
OriginalWord = false;
}
}