If I have the following code

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

int main(void)
{
  char prose[] = "to be or not to be that is the question";
  int  i, len;
  
  len = strlen(prose);

  puts (prose);

  return 0;
}

where would I insert the function Cap to capitalize the first letter of each word? Am I better off using a different method?

#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
int main(void)
{
  char prose[] = "to be or not to be that is the question\n";
  char *ptr; 
  ptr = prose;
  puts (prose);
  *ptr = *ptr-32;
  printf("The new text with starting letter CAPS is \n");
  puts(prose);
  return 0;
}

OUTPUT:

to be or not to be that is the question

The new text with starting letter CAPS is 
To be or not to be that is the question

You can't. It's a static string and cannot be modified.

Aroo? prose is an array initialized with a string literal, it can most certainly be modified.

>*ptr = *ptr-32;

Good god, are people still doing this? The world is not ASCII anymore, folks, use toupper() if you want to be one of the cool kids.

Thanks for the help. :) To Narue: toupper certainly is a little more elegant, but i'm studying with a really old school c programmer who kind of wants me to go through the basics [thinks it will encourage good practices] so he probably would want to see me use *ptr ...

To Arin: Close, but I have to write a function that will capitalize the first letter of each word in the string. Grammatically incorrect, I know. :p Though, the proposed solution does get the wheels turning a little bit.

You will have to check each character to find the spaces and/or tabs, then convert the next non-white-space character to upper case (there could be more than one white-space characters between words). You have to use a loop to do that.

As for toupper(), check your compiler to see if it supports it. Almost all C compilers do. Don't know about Turbo C but I suspect it does too.

Edited 5 Years Ago by Ancient Dragon: n/a

i'm studying with a really old school c programmer

While his code may be shit, I wouldn't recommend inheriting that particular trait.

who kind of wants me to go through the basics [thinks it will encourage good practices] so he probably would want to see me use *ptr ...

Yeah? How about *ptr = toupper(*ptr); ? Using pointer operations for educational purposes doesn't excuse otherwise bad code in this case.

Edited 5 Years Ago by Narue: n/a

Comments
Yes

I know I probably have to use some sort of while loop to search for a null.

I know I probably have to use some sort of while loop to search for a null.

Yes, you're searching the string for words, so some sort of loop will be involved. Think about how you might do it manually given only a look at one character at a time.

Given "this is a test":

't': It's not whitespace. We're not in a word, so capitalize it.
'h': It's not whitespace. But we're in a word, so leave it.
'i': It's not whitespace. But we're in a word, so leave it.
's': It's not whitespace. But we're in a word, so leave it.
' ': It's whitespace, so we're not in a word anymore.
'i': It's not whitespace. We're not in a word, so capitalize it.
's': It's not whitespace. But we're in a word, so leave it.
' ': It's whitespace, so we're not in a word anymore.
'a': It's not whitespace. We're not in a word, so capitalize it.
' ': It's whitespace, so we're not in a word anymore.
't': It's not whitespace. We're not in a word, so capitalize it.
'e': It's not whitespace. But we're in a word, so leave it.
's': It's not whitespace. But we're in a word, so leave it.
't': It's not whitespace. But we're in a word, so leave it.
'\0': String terminator, we're done!

So what do you need to accomplish this algorithm? You need to be able to determine if a single character denotes whitespace, and you need to know if you're presently in a word.

Yes , i agree with everyone that isupper is a modern solution....but i just wanted toshow how pointers are helpful in these situation..Yes you have to find the blanks and then convert in Capital every word following the blank......You can do it by again pointer or the isupper.......I suggest you practice both.....We should know all kind of approaches....Thanks for the suggestions everyone.....Theres a lot i learn from here.

i just wanted toshow how pointers are helpful in these situation

Pointers are irrelevant in this situation, which suggests that you don't really understand either how pointers work, how this solution works, or a combination of the two.

I was under the impression that there was a cap function that came with the language when using the appropriate header file & that he had wanted me to invoke it. I'm not taking a course for a grade so I suppose I could find my own way to get from point a to point b. There's enough info here for me to start tho. Thanks, guys.

This board = kinda awesome.

Narue, you don't think you're being a little bit harsh in a place where people come to learn? You have been accusatory toward everyone w/o even offering a solid solution.

I was under the impression that there was a cap function that came with the language when using the appropriate header file & that he had wanted me to invoke it.

There is -- its called toupper() but it only converts a single character and you have to tell it which character to convert. Otherwise, there is no such standard function in the C or C++ languages.

Narue, you don't think you're being a little bit harsh in a place where people come to learn?

Nope.

You have been accusatory toward everyone w/o even offering a solid solution.

I gave you a complete walkthrough of the algorithm. Any more than that would be spoon feeding you the solution. Let's also consider that you've offered no proof that you're putting any effort into this exercise, so I'm disinclined to be generous.

To your credit, you did walk me through the proposed function of said algorithm. But even still, you attract more flies w/ honey than vinegar if you have a point to get across. + there's no such thing as proof on the internet. I don't need your "generosity." Sweeten up a little bit and lose the i'm-the-only-female-programmer-in-the-world complex. If people are doing something in a way that is dated, deprecated, or just plain inefficient, there are ways to let them know without undermining them.

+ there's no such thing as proof on the internet

Posting code with your attempt at the algorithm is sufficient.

Sweeten up a little bit and lose the i'm-the-only-female-programmer-in-the-world complex.

Um...what? How does my gender have anything to do with your being overly sensitive?

Let go ari$av3s, i dont think she is understanding what you wana say............Its OK Narue..We dont blame you .......You can do things your way.....we can do ours.........ari$av3s-Keep learning sincerely...Do try to create your own solutions..you never know what might work........Nice talking to you all

This article has been dead for over six months. Start a new discussion instead.