0

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
patryk-szwed
  • 180
  • 1
  • 4
  • 12
  • 5
    `while((c = getchar()) != EOF)` is wrong. `c` needs to be `int` not `char`. https://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc – MFisherKDX Nov 12 '17 at 21:07
  • So what happens on input: `Aa`? Trace the code. – MFisherKDX Nov 12 '17 at 21:13
  • 2
    I don't clearly understand what you mean with "token" and "if strings there are names". Is a token a comma separated sequence of strings? Are strings enclosed in `"`-signs? Can you please give some sample input and expected output. – Stephan Lechner Nov 12 '17 at 21:18
  • Okay, I've changed c to int, thanks for the clue. @xing but that's what I want as Name is not set to true at this moment and I'm looking for its beginning. – patryk-szwed Nov 12 '17 at 21:23
  • 1
    ... and the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem. Show the input, the expected output, and the actual output as text *in the question*. – Weather Vane Nov 12 '17 at 21:26
  • @StephanLechner by token i meant a character from input. I'll provide an example in the question. – patryk-szwed Nov 12 '17 at 21:33
  • Poor Mr McDonald! His name isn't a name? – Jonathan Leffler Nov 12 '17 at 21:37
  • @JonathanLeffler you're right. But in that case it is stated that names begins with an uppercase and other letters are lower. Unfortunately for McDonald. – patryk-szwed Nov 12 '17 at 21:43
  • 1
    Your function doesn't null-terminate the string under some conditions. And it will malfunction if EOF is read by the innermost `while` loop – M.M Nov 12 '17 at 22:07
  • Still not a [MCVE], which should compile and exhibit the problem behavior; and you don't describe the problem behavior. "I have no idea why it does not work" is not a helpful description. What input do you give the program and what is the output when it fails? It does look like this code will report input such as "aAdam" as a name. Also, why not just return the boolean `name` from the function instead using the `if else` construction? – ad absurdum Nov 12 '17 at 22:12

1 Answers1

0

This code works as this task required.

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
int c;               //used for loading a char

while((c = getchar()) != EOF)
{
    if (!name && !isupper(c)) continue;     //There hasn't been any name in standard input yet

    if(!name && isupper(c))     //First char of a name
    {
        name = true;
    }

    else if(name && !islower(c))        //End of the name
    {
        buffer[i++] = '\0';
        return true;
    }

    //Override guard
    if(i == len - 1)
    {
        buffer[i++] = '\0';
        return false;
    }

    buffer[i] = c; //Add "c" to string
    i++;
}

//End of a file
if(name)
{
    return true;
}
return false;
}

PS: I'm sorry for not responding here. I was trying to fix this code. Thanks for your help.

patryk-szwed
  • 180
  • 1
  • 4
  • 12
  • _Does_ this code work as expected? For input `"MacDonald"`, only the `"Mac"` is stored in the buffer. For inputs `"aAdam"` and `"aaAdam"` the initial lowercase letters are truncated, so only `"Adam"` is stored in the buffer. For keyboard input with no uppercase letters, such as `"jonathan"`, the function falls into an infinite loop. Note that four `return` statements in one function is bad style and a sure sign that your function should be rethought. – ad absurdum Nov 13 '17 at 03:34
  • To be honest, I don't know exactly as the description of this task wasn't clear enough. As a result, I couldn't clearly describe it here. But this code was the one which he wanted. – patryk-szwed Nov 13 '17 at 16:04