First figure out how to tell the first character of a word.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char *ch, text[] = "The quick brown fox jumps over the lazy dog.";
int word = 0;
for ( ch = text; *ch; ++ch )
{
if ( isalpha(*ch) )
{
if ( !word )
{
word = 1;
printf("The start of a new word is '%c'\n", *ch);
}
}
else
{
word = 0;
}
}
return 0;
}
/* my output
The start of a new word is 'T'
The start of a new word is 'q'
The start of a new word is 'b'
The start of a new word is 'f'
The start of a new word is 'j'
The start of a new word is 'o'
The start of a new word is 't'
The start of a new word is 'l'
The start of a new word is 'd'
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>and a function to convert the first letter of the string 't' to 'T'
No. You have a function to convert the entire string to uppercase. (That is, it would if you would increment the pointer so the loop would eventually terminate.) If you are not intending to loop, you function becomes even more trivial.
void convertToUpperCase(char *sPtr)
{
*sPtr = toupper(*sPtr);
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>*sPtr = toupper(*sPtr);
*sPtr = toupper ( (unsigned char)*sPtr );
Since you can't be sure that *sPtr was a character returned by a standard input function. Better safe than sorry in these situations. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>*sPtr = toupper(*sPtr);
*sPtr = toupper ( (unsigned char)*sPtr );
Since you can't be sure that *sPtr was a character returned by a standard input function. Better safe than sorry in these situations. ;)
Since I had to refresh my memory, I'll let everyone else in on it too. http://www.stanford.edu/~blp/writings/clc/ctype-cast.html
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>When i do a for loop yes it prints the string but every word becomes a capital letter.
If you only want the first letter to be capitalized, only call the function on the first character:
char s[] = "this is a test";
s[0] = toupper ( s[0] );
printf ( "%s\n", s );
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
void covertToUppercase(char title);
This is different from...
void covertToUppercase(char *title);
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
You're passing it a pointer, as the error message states.
covertToUppercase(title);
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>what if i want to convert the first letter of each word in the string to uppercase?
You convert the first character and the first character after whitespace to upper case:
void first_word ( char *s )
{
bool upper = true;
while ( *s != '\0' ) {
if ( upper && !std::isspace ( (unsigned char)*s ) ) {
*s = std::toupper ( (unsigned char)*s );
upper = false;
}
else if ( !upper && std::isspace ( (unsigned char)*s ) )
upper = true;
*++s;
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>how do i fix that?
It was written for C++. Include instead of and compile as C++. If that doesn't work, get a newer compiler. If that's not an option, just remove the std:: parts until it compiles.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401