Hi.I have written this code which capitalizes 1st character of each word in the string.All other letters of the word has to be in small letter.But i am not getting desired output.Plz tell me where i am going wrong..

#include<stdio.h>
int main(void)
{
 char string[]="hI i aM sOujanYa";
 char string2[20]="";
 char *ptr;
 int i;
 ptr=&string[0];
 string2[0]=*ptr;
 for(i=0;*ptr!='\0';*ptr++,i++)
 {
  if(*ptr>='a' && *ptr<='z')
  *ptr=*ptr-32;
  string2[i]=*ptr;
  *ptr++;
  i++;
  while(*ptr!=' '&& *ptr!='\0')
   {
     if(*ptr>='A'&& *ptr<='Z')
     *ptr=*ptr+32;
     string2[i]=*ptr;
     *ptr++;
     i++;
   }
 }
 string2[i]='\0';
 puts(string2);
 }

Edited 4 Years Ago by soujanya.bhat.184

for(i=0;str[i]!='\0';i++)
{
if(str[i]==32)
{
i++;
printf("%c",str[i]);
}
}

hello u can try like this where ascii value of a space is 32

Plz tell me where i am going wrong..

Start with the fact that you're double incrementing both ptr and i. Note how the increment clause of the for loop increments both of them, but in all other paths of execution in the body you still increment both of them.

Fix that and then trace through the execution in a debugger to see how your casing logic is actually working compared to how you think it works.

hello u can try like this where ascii value of a space is 32

Why are you using 32 when there's a character literal (ie. ' ') that doesn't depend on ASCII or ASCII-compatible character sets?

@OP: Quoting deceptikon:

Fix that and then trace through the execution in a debugger

Did you try what he suggested?
Do you not have a debugger?
AFAIK, even very old IDEs come with a debugger wherein you can add watches and do some debugging.

In the worst case that you have no debugging environment, try adding printf() messages after each step to get meaningful details that would enable you to find the problem.

Edited 4 Years Ago by myk45

@deceptikon Can u plz post the corrected code??

No. You wouldn't learn squat if I did the work for you.

Edited 4 Years Ago by deceptikon

I tried removing the increment part in the for loop.But still not working.Only 1st word is transformed as required.Not other words.

I am getting the output like this."Hi i am soujanya".Except for the first word all other words are transformed into small lettered words.

I am getting the output like this."Hi i am soujanya".Except for the first word all other words are transformed into small lettered words

Paste the modified code here.

#include<stdio.h>
int main(void)
{
 char string[]="hI i aM sOujanYa";
 char string2[20]="";
 char *ptr;
 int i;
 ptr=&string[0];
 string2[0]=*ptr;
 for(i=0;*ptr!='\0';)
 {
  if(*ptr>='a' && *ptr<='z')
  *ptr=*ptr-32;
  string2[i]=*ptr;
 *ptr++;
  i++;
  while(*ptr!=' '&& *ptr!='\0')
   {
     if(*ptr>='A'&& *ptr<='Z')
     *ptr=*ptr+32;
     string2[i]=*ptr;
     *ptr++;
     i++;
   }
 }
 string2[i]='\0';
 puts(string2);
 }

Edited 4 Years Ago by soujanya.bhat.184

1) indent better so we can read the code easier. 3 to 4 spaces is normal.
2) print your code and grab another piece of paper and a pencil.
3) sit at your desk and start at the top of your code and start writing out all the variables (including string sizes) and follow your code step by step, changing variables values and drawing the arrows to where the pointers point.

You should then see where you are going wrong and, if you can't figure out how to fix it, you can ask a really good question because you can explain in detail what you saw happening.

no deceptikon i have done this task in past and it worked for me actually the output will be like dis

OUTPUT
ENTER THE LETTERS: RANDOM ACCESS MEMORY
THE OUTPUT IS :RAM

dis is wat i did. r u asking this task soujanya.bhat.184??????

no deceptikon i have done this task in past and it worked for me actually the output will be like dis

Sorry, I'm having trouble deciphering the meaning of this sentence. Are you sure you're responding to something I said? If so, please quote it. Also, it would be nice if you used full sentence English rather than txt speak. Is it really so hard to type "this" instead of "dis"? Or "are" instead of "r"? Or "you" instead of "u"?

@ ritish

How will this work for more than 1 spaces between two words ?

@ soujanya

Use isspace() to check where you are getting spaces, if you get space, use a loop where the condition will read " continue looping untill isspace() returns false value " . Where the loop stops, that is the starting letter of a word. You can check if its really a alphabet or not with isalpha() ( making the code more efficient ). Then change that letter to upper using toupper().

Here's what I think your algorithm is:

Point to the first character
Loop until the end of the string
    If current char is lower case
        make char upper case
    End if
    Copy character
    Point to next character
    Loop until current char is space or end of string
        If char is upper case
            make char lower case
        End if
        Copy character
        Point to next character
    End loop
End loop
Terminate string copy
Display converted string

If you follow the advice WaltP gave earlier:

3) sit at your desk and start at the top of your code and start writing out all the variables (including string sizes) and follow your code step by step, changing variables values and drawing the arrows to where the pointers point.

... you should be able to see what the problem is pretty quickly. You're already close. Try WaltP's suggestion. Figure out which character is being pointed to at each step in the algorithm for the input string.

Edited 4 Years Ago by Bob

Yippie.....Finally i got it...I had forgotten to include another condition to check for space...thank you all for your suggestions...here is my new code.....suggestions about any improvements are welcome....

#include<stdio.h>
int main(void)
{
 char string[]="hI i aM sOujanYa";
 char string2[20]="";
 char *ptr;
 int i;
 ptr=&string[0];
 string2[0]=*ptr;
 for(i=0;*ptr!='\0';)
 {
     if(*ptr==' ')
     {
         string2[i]=*ptr;
         *ptr++;
         i++;
     }
  if(*ptr>='a' && *ptr<='z')
  *ptr=*ptr-32;
  string2[i]=*ptr;
 *ptr++;
  i++;
  while(*ptr!=' '&& *ptr!='\0')
   {
     if(*ptr>='A'&& *ptr<='Z')
     *ptr=*ptr+32;
     string2[i]=*ptr;
     *ptr++;
     i++;
   }
 }
 string2[i]='\0';
 puts(string2);
 }
This question has already been answered. Start a new discussion instead.