1

So people told me that I can't use an array of strings and try to output just the first character of every single string like in this code for example:

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

int main(void)
{   
    string s[] = GetString();

    for(int i = 0, k = strlen(s); i < k; i++)
    {
        for(int n = 0; n <= i; n++)
        {
            int j = 0;

            if(s[i][j] >= 'a' && s[i][j] <= 'z')
            {    
                printf("%c\n", s[i][j]) - ('a' - 'A');
            }
            else
            {
                printf("%c\n", s[i][j])
            }
        }
    }
}

So, I'm hoping you guys give me a tip on how to type every first letter of every single word given as an input without differentiating every word as a new string and then taking its first char. I know I have to leave the code like this :

string s = GetString();

But then how would I command the programme to type out just the first letter of every new word if the computer doesn't see them as different elements but as a whole forming one big string.

1 Answer 1

2

Parse the string (array of chars) with a for loop. If found a space then print the following char (whether or not you check if that next char is alphabetic is up to you).

A little piece of code like this would find, capitalize, and print chars that come after a space for you. (ctype.h library)

if (name[i] == ' ')
{
    printf("%c", toupper(name[i + 1]));
}
2
  • when I tried this I got an error due to my name being a string and not a character. But I thought that string name would make name a string BUT name[1] for example would be converted to a character type. How do I convert name 1 from a string to a character? Aug 26, 2016 at 3:23
  • name[1] is already a char Aug 28, 2016 at 3:25

You must log in to answer this question.

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