Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversion

Status
Not open for further replies.

lisalis99

Programmer
Jan 9, 2005
121
US
Hi All!

I'm trying to get this code to work where when I type a string in, it will take the first character of each word and make it uppercase and the rest lower case-- I can't get it to work- I got it to go all uppercase?!!! How to get it so it only takes the first character to uppercase and rest lower? Any ideas?

#include <stdio.h>

#include <string.h>

#include <ctype.h>

int main(void)


{

char line[40];

char *pc;

int length;

while(1)

{

if(gets(line) == NULL)

{

printf("End of file encountered.\n");

break;

}

if (strcmp(line, "quit") == 0)

{

printf("Good bye!\n");

break;

}

length = strlen(line);

for (pc = line; pc - line < length; pc++)

{

putchar(toupper(*pc));

}

putchar('\n');

}

return 0;

}

 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
pc=line;

for (x=0; x < 2; x++)

{

putchar(toupper(*pc));
pc++;

}
 
Code:
pc=line;
for (x=0; x < 2; x++)
{
putchar(toupper(*pc));
pc++;
}
 
Code:
pc=line;
for (x=0; x < 2; x++)
{
*pc = toupper(*pc);
pc++;
}

then print the string
 
Thanks -- I will try it -- sorry about the code thingie- I'm a newbie to this forum! Appreciate the help.
 
don't feel so bad, been here a long time, and just figured it out tooo.
 
Oh, I did that-- when I type my string: (Any String)

EX:

MY DOG IS GREAT

I only get 2 letters both capitalized-- I need it to read:

My Dog Is Great

Thanks-
Lisa
 
I'm stuck-- I put that code in the place of what I had and when Enter a String comes up- nothing happens!! Help!!
 
Code:
int capitalize=1;
pc=line;
for (x=0; x < length; x++)
{
if (*pc==' ')
    {
      capitalize=1;
      continue;
     }
if (capitalize==1)
     {
      *pc = toupper(*pc);
      capitalize=0;
     }
else
{
*pc = tolower(*pc);
} 
pc++;
}
 
Hi--not sure about the bug-- I'm new to C; I wish I could figure it out.

Here is my latest code- I put it back to the original because it wasn't doing anything when I typed my string in-

Code:
#include <stdio.h>

#include <string.h>

#include <ctype.h>

int main(void)


{

char line[40];

char *pc;
int x;

int length;

printf ("\nEnter a string: ");

while(1)

{

if(gets(line) == NULL)

{

printf("End of file encountered.\n");

break;

}

if (strcmp(line, "quit") == 0)

{

printf("Good bye!\n");

break;

}

length = strlen(line);

for (pc = line; pc - line < length; pc++)

{

putchar(toupper(*pc));

}

putchar('\n');

}

return 0;

}
 
Well, your code does not capitalize anything... Why?..
Try this:
Code:
char* Capitalize(char* str)
{
  char* p = str;
  int   ch;

  if (p) /* arg check guard */
  {
     while ((ch=*p) != 0) /* end of string? */
     {
        if (isalpha(ch)) /* next word encountered */
	{
           *p++ = toupper(ch); /* 1st letter, go on */
           while (ch=*p, isalpha(*p)) /* tail to lower */
             *p++ = tolower(ch);
        }
        else /* it's not a letter */
          ++p;
     }
  }
  return str;
}

int main()
{
  char line[120]; /* More than 40, 40 is too small... */
  /* Always explain the program function (more friendly ) */
  printf("Let\'s capitalize (type quit to quit)...\n");

  while(printf("Enter a string: ") > 0) /* do prompt! */
  {
    if (fgets(line,sizeof line,stdin) == NULL)
    {
       printf("End of file encountered.\n");
       break;
    }
		
    if (strcmp(line,"quit\n") == 0) /* fgets gets newline */
    {
       printf("Good bye!\n"); /* That's OK! */
       break;
    }

    printf("%s",Capitalize(line)); /* Have a string, doit */
    /* No \n in the format: it's in arg string (fgets!) */
  }
  return 0;
}
Of course, you may use gets(), but it's a very dangerous routine: possible buffer overflow. The fgets() is more, more robust...
Old good C principle: Have a functional specification? Make a function!..
See the 2nd while condition in Capitalize(): comma operation (do it, do that) is a very useful one on some circumstances...
Good luck!
 
English is much easier to understand than code.

Let's think about what you need to do to get the capitalization you want. Let's assume that, for your purposes, words are separated by spaces.

Forget about the first word for a second, and pretend you're at a spot in the middle of the string. How can you find the start of the next word?


(1). Well, since words are separated by spaces, your first step is to search for the next space in the string.

If there are no more spaces left in the string, that means you're past the last word of the string and you're done.


When you find a space, though, then the next non-space character is the start of the next word, right?

(2). So now, you just need to walk over to the next non-space character. (There might be more than one space, so it might not be the very next one).

However, as you walk, you should check to make sure that the string doesn't end from under you, in case someone left spaces at the end of the string they entered and there is no next word.

When you find the next non-space character, you can capitalize it.


Then, you'll need to start looking for the next space again.


Finally, let's think about that first word. What happens if we start off on step 2: finding the next non-space character. That should work for the first character, too, right?

So maybe I gave you those steps out of order. They're easier to understand that way, though.


Now, a point about what you're doing: you're walking along the string and outputting the capitalized version of each letter.

An alternate strategy would be to edit the string in place. That is, to walk along the string changing some letters of the string. Then, output the modified string when you're done.

The reason I suggest that is that, if you do it that way, you can use a few C functions that'll make walking the string a bit easier.


For example, when you're looking for the next space, you could use the [tt]strchr[/tt] function.

[tt]strchr(str, ch)[/tt] returns a pointer to the next occurrence of the character [tt]ch[/tt] in [tt]str[/tt], or [tt]NULL[/tt] if there aren't any more.


And when you've found a space, you can find out how far it is to the next non-space character by using the [tt]strcspn[/tt] function.

[tt]strcspn(str, chars)[/tt] returns the distance from [tt]str[/tt] to the first character that isn't in the string [tt]chars[/tt].

E.g. [tt]strcspn(" elbow", " ")[/tt] would return 2, since there are two spaces at the beginning of the string.


Your C library may have some other, more convenient string functions available. Check the documentation for a function that does what you want, or ask back here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top