0

How to get rid of spaces in the end of the input? My code works perfectly with _ _ milla_ _ _ banana But I have issues with the case if _ _ _ milla_ _ banana_ _ _ when I have spaces in the end

How have you fixed this problem? now, I am actually desperate :(

int main(void)
{
    int next = 0;  //is used to skip many spaces in the beginning

    string name = GetString();

    //make upper-case
    for (int i = 0, n = strlen(name) ; i < n; i++ )
    {
     if (name[i] >= 'a' && name[i] <= 'z')
        {
            name[i] = name[i] - 32; // 32 ?
        }
    }

    if (name != NULL)
    {
        // write the first symbol   // if   _ _  _  _ banana
        // to write B from _ _ _ banana_ _ _ _ apple
        for (int i = 0, n = strlen(name); i < n; i++ )
        {
            if (name[i] == 32)
            {
                continue;   
            }
            else if (name[i] != 32 )
            {
                printf ("%c", name[i]);
                next = i;   //skip spaces in the beginning
                break;
            }
        }
        // to write A from _ _ _ banana_ _ _ _ apple
        for (int i = next, n = strlen(name) ; i < n; i++ )
        {
            while (name[i] == 32)
            {    
                if (name[i] == 32)
                {
                    i++;
                    continue;
                }
            }

            if (name[i-1] == 32 && next != i ) // not to write name[0] again
            {
                printf ("%c", name[i]);
            }
        }    
    }
    printf ("\n");
}
0

if someone is interested in

for (int i = next + 1, n = strlen(name) ; i < n; i++ )
{
            while (name[i] == 32)
            {    
                i++;

                if (name[i] != 32 && i < n)  
                {
                    printf ("%c", name[i]);
                }
            }
}   

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .