0

please dont laugh at me as i am totally new to programming, and on top of that maybe not very smart. I am just enjoying a lot what i am learning though i am afraid i will not be able to finish the cs50 course cause it seems to hard. Anyway, im trying to solve the initials pset2. I can get first letter of first word in printed in capital, and thats it. I can t find the logic to go to next word. At the beginning i was going with argv and argc so at least i could isolate names and get to first letter. But with int-main-void i just cant seem to do it. Here is my code, any hint would be much appreciated

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

int main(void)


{
    //get a name

    string name = get_string();
    //make sure you get a word
    if (name != NULL)
    //print First capital Firts word
    printf("%c", toupper(name[0]));

    {

    for

    //loop duration until /0ahahah i wanna get after this \0
(int i = 0;name[i] == '\0'; i++ )

{ 
  if(name[i] == ' ')

       {
    //print  0 +1  character (next after space)
    printf("%c", toupper (name[i]+1));

}

}
 printf("\n");
}}

after Blauelf suggestion

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

int main(void)


{
    //get a name

    string name = get_string();
    //make sure you get a word
    if (name != NULL)
    //print First capital Firts word
    printf("%c", toupper(name[0]));

    {

    for

    //loop duration until /0ahahah i wanna get after this \0
(int i = 0;name[i] != '\0'; i++ )

{ 
  if(name[i] == ' ')

       {
    //print  0 +1  character (next after space)
    printf("%c", toupper (name[i+1]));

}

}
 printf("\n");
}}

its working but i dont get why.It should stop after the first inputted word, aint it?

1

A few things:

Print toupper(name[0]) only if it's a letter (and not a leading space).

The condition in a for loop should be non-zero (true is non-zero for example) if you want to keep looping, so use ; name[i] != '\0'; instead of ==.

Print the character following a space (name[i + 1], not name[i] + 1) only if it is a letter and not a second space character or the null terminator.

| improve this answer | |
  • thanks Blauelf, really appreciate your help. – idbeentaken Jul 29 '17 at 0:02
  • I understand what you mean about the condition, dont get the first thing you told me though; what d you mean by LEADING space? (italian is my mothertongue, sorry maybe i just dont get the english meaning) – idbeentaken Jul 29 '17 at 0:03
  • It works now Blauelf, but funny thing is I dont get why... It feels to me i just stated to print first letter of the first inputted word, but not the second. Why it doesnt stop after the first word? – idbeentaken Jul 29 '17 at 0:06
  • #include <stdio.h> #include <cs50.h> #include <string.h> #include <ctype.h> int main(void) { //get a name string name = get_string(); //make sure you get a word if (name != NULL) //print First capital Firts word printf("%c", toupper(name[0])); { for //loop duration until /0ahahah i wanna get after this \0 (int i = 0;name[i] != '\0'; i++ ) { if(name[i] == ' ') { //print 0 +1 character (next after space) printf("%c", toupper (name[i+1])); } } printf("\n"); }} – idbeentaken Jul 29 '17 at 0:07
  • It stops as soon as it hits the string end (which is marked by a '\0'), as that's what you tell it in the for loop's condition. If that code passes, you have neither leading space (space characters before the first word), nor multiple spaces at once, nor trailing spaces (space characters after the last word). Those are probably for the "more comfotable" version then. – Blauelf Jul 30 '17 at 11:44

You must log in to answer this question.

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