| | |
Convert first character from lowercase to uppercase?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2005
Posts: 5
Reputation:
Solved Threads: 0
Hmm...how do i go about doing a conversion only the first letter of the string?
<< Thread split from original since this is taking on a life of its own. >>
<< Thread split from original since this is taking on a life of its own. >>
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'
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Apr 2005
Posts: 5
Reputation:
Solved Threads: 0
Hi,
I have declared a string:
and a function to convert the first letter of the string 't' to 'T'
<< moderator edit: added [code][/code] tags >>
Is not working tho...can someone help me on this problem? Thank you!!!!
I have declared a string:
C Syntax (Toggle Plain Text)
char title[MAXTITLE] = "the Empire Strikes Back";
and a function to convert the first letter of the string 't' to 'T'
C Syntax (Toggle Plain Text)
void convertToUpperCase(char *sPtr) { while(*sPtr != '\0') { if (islower(*sPtr)) *sPtr = toupper(*sPtr); } }
Is not working tho...can someone help me on this problem? Thank you!!!!
>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.
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.
C Syntax (Toggle Plain Text)
void convertToUpperCase(char *sPtr) { *sPtr = toupper(*sPtr); }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>*sPtr = toupper(*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.
C Syntax (Toggle Plain Text)
*sPtr = toupper ( (unsigned char)*sPtr );
New members chased away this month: 3
•
•
•
•
Originally Posted by Narue
>*sPtr = toupper(*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.C Syntax (Toggle Plain Text)
*sPtr = toupper ( (unsigned char)*sPtr );
http://www.stanford.edu/~blp/writing...type-cast.html
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Apr 2005
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>*sPtr = toupper(*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.C Syntax (Toggle Plain Text)
*sPtr = toupper ( (unsigned char)*sPtr );
>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:
If you only want the first letter to be capitalized, only call the function on the first character:
C Syntax (Toggle Plain Text)
char s[] = "this is a test"; s[0] = toupper ( s[0] ); printf ( "%s\n", s );
New members chased away this month: 3
•
•
Join Date: Apr 2005
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>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:
C Syntax (Toggle Plain Text)
char s[] = "this is a test"; s[0] = toupper ( s[0] ); printf ( "%s\n", s );
C Syntax (Toggle Plain Text)
#include <iostream> #include <cctype> #define MAXTITLE 128 using namespace std; void PrintCharacters(const char *); void covertToUppercase(char title); int main() { char title[MAXTITLE] = "the Empire Strikes Back"; //cout << "Enter title: "; //cin >> title; cout << "Before converting to Upper Case: "; PrintCharacters(title); cout << endl; cout << "After converting to Upper Case: "; covertToUppercase(title); cout << endl; return 0; } void PrintCharacters(const char * sPtr) { for(; *sPtr != '\0'; sPtr++) cout << *sPtr; } void covertToUppercase(char title) { title[0] = toupper(title[0]); cout << title; /* for (; *sPtr !='\0'; sPtr++) { *sPtr = toupper ((unsigned char) *sPtr); cout << *sPtr; } */ /* while (*sPtr != '\0') { if (islower(*sPtr)) *sPtr = toupper((unsigned char)*sPtr); cout << *sPtr; sPtr++; } */ }
C Syntax (Toggle Plain Text)
titles.cpp: In function `int main()': titles.cpp:22: error: invalid conversion from `char*' to `char' titles.cpp:22: error: initializing argument 1 of `void covertToUppercase(char)' titles.cpp: In function `void covertToUppercase(char)': titles.cpp:36: error: invalid types `char[int]' for array subscript titles.cpp:36: error: invalid types `char[int]' for array subscript
Sorry to bother you guys again, can you kindly explain where have i gone wrong? I know it will be easier if i could use string libraries but i ain't allowed to do so. Can you kindly help me out on this? Thank you.
C Syntax (Toggle Plain Text)
void covertToUppercase(char title);
void covertToUppercase(char *title); "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Counting the Number of Lines in File
- Next Thread: how to create a vector of type structure
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi





