Q. WAPC to read a string into an aaray and capitalize the first character of every word in the input.

Solution I tried

#include<stdio.h>
int main(void)
{
char string[50];
char *ptr;
printf("Enter string\n");
fgets(string,sizeof string,stdin);
printf("string= \"%s\"\n",string);
for(ptr=string;string!='\0';ptr++)
 {
 if(*ptr==' ')
  {
  ptr++;
  *ptr=*ptr-32;
  }
 }
printf("The new string is %s",string);
return 0;
}

What have I done wrong?????

Recommended Answers

All 4 Replies

You're getting the segmentation fault for because of this:

for(ptr=string;string!='\0';ptr++)

Try replacing 'string' with *ptr (and remember, the * is important for dereferencing the value contained in the pointer).

As for your logic -- it's a bit skewed. For one thing, you never consider the fact that the first letter of a word may already be capitalized. Oops, now you've just made that capital letter some who-knows-what.

Other problem you're going to have is that you only capitalize after spaces. What about the starting word in the string? Or is that not important?

2 functions that will solve both problems really easily (assuming of course, that you've already learned about these):

  • [search]strtok[/search] will split up the string into manageable sections, including the first word. All you have to do is capitalize the first character of each string that strtok() spits out.
  • [search]toupper[/search] will make a character uppercase. If it's already uppercase, it just leaves it that way.
#include<stdio.h>

int main(void)
{
    char string[50];
    char *ptr = string;

    printf("Enter string\n");
    fgets(string,sizeof string,stdin);

    printf("string= \"%s\"\n",string);

/* first letter of string */
    *ptr = toupper(*ptr);

/* going thru string */
    while(*ptr)
    {
        if( isspace(*ptr) )
        {
            *(++ptr) = toupper(*ptr);
        }
        ++ptr;
    }
    printf("The new string is %s",string);
    
    getchar();
    return 0;
}

>*ptr = toupper(*ptr);
You forgot to include ctype.h.

>/* first letter of string */
This is a benign logic error. The end result is that it doesn't cause a problem if the first character in the string isn't a letter, but the fact that you always do it suggests a flaw in your thinking.

>/* going thru string */
This is not a benign logic error. More than one space between words will produce unexpected output. You need to consider that a word could be anywhere in the string and surrounded by anything:

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

int main ( void )
{
  char buffer[BUFSIZ];

  printf ( "Enter a string: " );
  fflush ( stdout );

  if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
    size_t i = 0;

    /* Strip the optional newline */
    buffer[strcspn ( buffer, "\n" )] = '\0';

    /* Capitalize the first letter of every word */
    for ( ; ; ) {
      /* Ignore spans of whitespace */
      while ( buffer[i] != '\0' && isspace ( buffer[i] ) )
        ++i;

      if ( buffer[i] == '\0' )
        break;

      buffer[i] = (char)toupper ( buffer[i] );

      /* Ignore spans of non-whitespace */
      while ( buffer[i] != '\0' && !isspace ( buffer[i] ) )
        ++i;
    }

    printf ( "|%s|\n", buffer );
  }

  return 0;
}

It's easy to translate something like that into pointer arithmetic, if you're into that sort of thing.

I got it! Thanx a lot joey and aia! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.