Home > database >  Capitalize every word in a string when there are multiple white spaces
Capitalize every word in a string when there are multiple white spaces

Time:08-14

I am trying to capitalize every word in and here is my code:

char *cap_string(char *str)
{
    int i;

    for (i = 0; str[i] != '\0'; i  )
    {
        if (i == 0)
        {
            if (str[i] >= 'a' && str[i] <= 'z')
                str[i] -= 32;
            continue;
        }
        if (str[i] == ' ')
        {
              i;
            if (str[i] >= 'a' && str[i] <= 'z')
            {
                str[i] -= 32;
                continue;
            }
        }
        else
        {

            if (str[i] == '.')
            {
                  i;
                if (str[i] >= 'a' && str[i] <= 'z')
                {
                    str[i] -= 32;
                    continue;
                }
            }
        }
    }

    return (str);
}

My question is that my code works fine in most cases, but does not function properly if it encounters multiple white spaces. How can I capitalize a word preceded by multiple white spaces?

CodePudding user response:

Change your code to the following:-

char *cap_string(char *str) 
{
    int i;

    for (i = 0; str[i] != '\0'; i  ) 
    {
        if (i == 0 || str[i - 1] == ' ' || str[i - 1] == '.') 
        {
            if (str[i] >= 'a' && str[i] <= 'z')
                str[i] -= 32;
        }
    }

    return (str);
}

Testing all cases, using the following code,

#include <stdio.h>

int main()
{
    char str[] = "hello  world.hello";
    printf("%s", cap_string(str));

    return 0;
}

returns

Hello  World.Hello

I have tried to keep your logic intact and not use any string.h library functions.

You have to keep in mind that the other conditions after the || operator are not checked if the first condition is evaluated as true. So str[-1] never occurs.

CodePudding user response:

The main idea is look at the previous letter to see if the current letter has to upper case. Introduced a constant UPCASE_AFTER so it's easy to add other punctuation marks (say, '!', '?'). Added test case. Refactored for readability.

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define UPCASE_AFTER " ."

char *cap_string(char *str) {
    for (int i = 0; str[i]; i  ) {
        if (!i || strchr(UPCASE_AFTER, str[i-1])) {
            str[i] = toupper(str[i]);
        }
    }
    return str;
}

int main() {
    printf("%s\n", cap_string((char []) {"a bb  c.d.ee..f."}));
    return 0;
}

and it returns:

A Bb  C.D.Ee..F.

CodePudding user response:

Here's another alternative:

#include <stdio.h>
// Use standard library routines
// like 'isalpha()', 'toupper'...
// Not all character sets have ASCII's contiguous alphabets.
#include <ctype.h>

char *cap_string( char *str ) {
    bool flag = true; // 'state flag' indicating "hunting for lowercase letter"

    for( char *cp = str; *cp; cp   )
        if( flag && islower( *cp ) ) {
            *cp = (char)toupper( *cp );
            flag = false;
        }
        else
            flag = !isalpha( *cp );

    return str;  // 'return' is not a function call
}

int my_main() {
    char str[] = "what? now is   the time   (we say sometimes) to learn C.";
    printf( "%s\n", cap_string( str ) );

    return 0; // 'return' is not a function call
}

Output

What? Now Is   The Time   (We Say Sometimes) To Learn C.
  • Related