0

This is my code

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



int main(){
   int i=0;
   printf("");
   char* str = GetString();

   printf("%c",*str);
   while(str[i]!='\0'){
          if ((str[i] > 96) && (str[i] < 123)){
            str[i]=str[i]-32; 
      }

      if(str[i]==' '){
            i++;
            printf("%c\n",*(str+i));
       }
       i++;

   }
   return 0;
}

However, I can't figure out how to convert 1 character from lowercase to uppercase. I know this may seem like an easy error but could someone help me out?

Edited code

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



int main(){
   int i=0;
   printf("");
   char* str = GetString();

   printf("%c",toupper(*str));
   while(str[i]!='\0'){
      if(str[i]==' '){
            i++;
            printf("%c\n",toupper(*(str+i)));
       }
       i++;

   }
   return 0;
}

still doesnt work

0

There are lots of helpful library functions. You should investigate isupper(), islower(), isalpha(), toupper() and tolower() and learn how they work. They will be a lot of help.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I have tried that but it doesnt seem to work properly. For example: #include <cs50.h> #include <stdio.h> #include <ctype.h> int main(){ int i=0; printf(""); char* str = GetString(); printf("%c",toupper(*str)); while(str[i]!='\0'){ if(str[i]==' '){ i++; printf("%c\n",toupper(*(str+i))); } i++; } return 0; }
    – Femfem
    Jul 29 '16 at 20:58
0

There's more than one way to do it, but the easiest way is to use the toupper() function. Try typing man toupper at a Terminal prompt in IDE50 to learn more about this, or just ask Google.

At this point in CS50, you should feel no shame in using the string datatype provided by cs50.h to simplify the task at hand. You will have plenty of opportunity to struggle with char* pointer arithmetic in later problem sets!

Also, you should think more carefully about what you want your code to do if(str[i]==' ') ... you don't want to print a space followed by a newline, which is what you're doing now.

You must log in to answer this question.

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