C function to capitalize first letter of words in an array

37,473

Solution 1

I took your code and tried to compile it. Well, it would be nice to see compilable code the next time. Here is one with comments.

#include <stdio.h> // Now I am able to use printf.
#include <string.h> // I was not able to use strlen without this...

void Cap(char string[]){     
    int i;
    int x = strlen(string); // You want to get the length of the whole string.
    for (i=1;i<x;i++){
         if (isalpha(string[i]) && string[i-1] == ' '){ 
         // only first letters of a word.
             string[i]= toupper(string[i]);
         }
    }
}

main(){
  char string[] = "text with lowercase words.";
  Cap(string);
  printf("%s",string);
};

Still the first word of the text is lowercase. This is a task for you.

Solution 2

you might want to run strlen(string) - as strlen(string[i]) is trying to get the length of a single char.

Solution 3

I will also point out your braces don't match ...

if (isalpha(string[i])){
       string[i]= toupper(string[i]);

Remove brace on the if line or put a close brace after your assigning statement.

Share:
37,473
4reel77
Author by

4reel77

Updated on December 26, 2020

Comments

  • 4reel77
    4reel77 over 3 years

    I'm pretty new to C and am hitting a wall when creating the below function. I want to use this function to make the first letter of a word upper case for a static character array (char string[]. It looks ok to my eye, but I'm getting some syntax errors which are probably pretty basic. compiler errors:

    error: invalid conversion from const char' toconst char*' initializing argument 1 of `size_t strlen(const char*)' assignment of read-only location

      void Cap(char string[]){
        int i;
        int x = strlen(string);
        for (i=1;i<x;i++){
             if (isalpha(string[i]) && string[i-1] == ' '){
             // only first letters of a word.
                 string[i]= toupper(string[i]);
             }if (isalpha(string[0]))
                            {
                              string[0]=toupper(string[0]);
                            }
             }
    }
    
  • 4reel77
    4reel77 over 10 years
    ok thanks for the reply, i do understand and fixed the first two errors, I will add that I want to pass a single sentence ie, char sentence [10]="this is a example" so char string[] would be ok?
  • tychon
    tychon over 10 years
    "words in an array" suggests it's an array of words, hence the confusion. Yes, you can use char string[] if you just want to capitalize the words in a string. You'll need to check for some sort of separation between words before you try to capitalize the first letter, however, as your current logic will just capitalize all letters in all words.
  • 4reel77
    4reel77 over 10 years
    ok i have added the fix for the first string in the array : if (isalpha(string[0])) { string[0]=toupper(string[0]); }
  • MacK
    MacK over 9 years
    Would be better if you expand your answer, explaining to the asker why his compiler is throwing that error and not just provide ready code.
  • Deanie
    Deanie over 7 years
    I voted this down because of a few errors. First the string starts with string[0] not string[1]. And your test, of checking the the character before is a ' ', doesn't work for the very first character in the string (as you'll be checking outside the variable, which could contain any character).
  • Robert Smith
    Robert Smith over 2 years
    Thank You. Don't forget to include: #include <ctype.h>