My task is to read a token into the buffer from a standard input and check if strings there are names. Names start with an uppercase and all other letters have to be lowercases. Here's my code.
bool nextName(char * buffer, int len)
{
bool name = false; //name = false checks if the loop is inside name
int i = 0; //number of a char in the name
char c; //used for loading a char
while((c = getchar()) != EOF)
{
while(c == ',' || c == '"') //cuts off commas
{
c = getchar();
}
if(name && !islower(c)) //If name is set to true and "c" is not a lowercase(it's an uppercase for example)
{ //it means it's not a name(I thought that will work)
buffer[i++] = '\0';
return true;
}
if(!name && isupper(c)) //First char of a name
{
name = true;
}
//Override guard
if(i == len - 1)
{
buffer[i++] = '\0';
return false;
}
buffer[i] = c; // Add char to the string
i++; // It's not "for loop" because incrementation can happen only when added
// char to string.
}
//End of file
if(name)
{
return true;
}
return false;
}
I thought that an if-statement
if(name && !islower(c)) //If name is set to true and "c" is not a lowercase(it's and uppercase for example)
{ //it means it's not a name
buffer[i++] = '\0';
return true;
}
will check if variable name is a proper name(one uppercase and others are lowercases). I have no idea why it does not work.
For input
It is an Example
I expect an output like this:
It
Example