Convert first character from lowercase to uppercase?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Closed Thread

Join Date: Apr 2005
Posts: 5
Reputation: Quickslvr is an unknown quantity at this point 
Solved Threads: 0
Quickslvr Quickslvr is offline Offline
Newbie Poster

Re: Convert first character from lowercase to uppercase?

 
0
  #1
Apr 28th, 2005
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. >>
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Convert first character from lowercase to uppercase?

 
0
  #2
Apr 28th, 2005
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
Quick reply to this message  
Join Date: Apr 2005
Posts: 5
Reputation: Quickslvr is an unknown quantity at this point 
Solved Threads: 0
Quickslvr Quickslvr is offline Offline
Newbie Poster

Re: Convert first character from lowercase to uppercase?

 
0
  #3
Apr 29th, 2005
Hi,

I have declared a string:

  1. char title[MAXTITLE] = "the Empire Strikes Back";

and a function to convert the first letter of the string 't' to 'T'

  1. void convertToUpperCase(char *sPtr)
  2. {
  3. while(*sPtr != '\0')
  4. {
  5. if (islower(*sPtr))
  6. *sPtr = toupper(*sPtr);
  7. }
  8. }
<< moderator edit: added [code][/code] tags >>

Is not working tho...can someone help me on this problem? Thank you!!!!
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Convert first character from lowercase to uppercase?

 
0
  #4
Apr 29th, 2005
>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.
  1. void convertToUpperCase(char *sPtr)
  2. {
  3. *sPtr = toupper(*sPtr);
  4. }
"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
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,833
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Convert first character from lowercase to uppercase?

 
0
  #5
Apr 29th, 2005
>*sPtr = toupper(*sPtr);
  1. *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.
New members chased away this month: 3
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Convert first character from lowercase to uppercase?

 
0
  #6
Apr 29th, 2005
Originally Posted by Narue
>*sPtr = toupper(*sPtr);
  1. *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/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
Quick reply to this message  
Join Date: Apr 2005
Posts: 5
Reputation: Quickslvr is an unknown quantity at this point 
Solved Threads: 0
Quickslvr Quickslvr is offline Offline
Newbie Poster

Re: Convert first character from lowercase to uppercase?

 
0
  #7
Apr 29th, 2005
Originally Posted by Narue
>*sPtr = toupper(*sPtr);
  1. *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.
I have tried this function, it did convert the letter 't' to 'T' however the output prints 'T' only and not the string. When i do a for loop yes it prints the string but every word becomes a capital letter.
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,833
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Convert first character from lowercase to uppercase?

 
0
  #8
Apr 29th, 2005
>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:
  1. char s[] = "this is a test";
  2.  
  3. s[0] = toupper ( s[0] );
  4. printf ( "%s\n", s );
New members chased away this month: 3
Quick reply to this message  
Join Date: Apr 2005
Posts: 5
Reputation: Quickslvr is an unknown quantity at this point 
Solved Threads: 0
Quickslvr Quickslvr is offline Offline
Newbie Poster

Re: Convert first character from lowercase to uppercase?

 
0
  #9
Apr 30th, 2005
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:
  1. char s[] = "this is a test";
  2.  
  3. s[0] = toupper ( s[0] );
  4. printf ( "%s\n", s );
  1. #include <iostream>
  2. #include <cctype>
  3. #define MAXTITLE 128
  4.  
  5. using namespace std;
  6.  
  7. void PrintCharacters(const char *);
  8. void covertToUppercase(char title);
  9.  
  10. int main()
  11. {
  12. char title[MAXTITLE] = "the Empire Strikes Back";
  13.  
  14. //cout << "Enter title: ";
  15. //cin >> title;
  16.  
  17. cout << "Before converting to Upper Case: ";
  18. PrintCharacters(title);
  19. cout << endl;
  20.  
  21. cout << "After converting to Upper Case: ";
  22. covertToUppercase(title);
  23. cout << endl;
  24.  
  25. return 0;
  26. }
  27.  
  28. void PrintCharacters(const char * sPtr)
  29. {
  30. for(; *sPtr != '\0'; sPtr++)
  31. cout << *sPtr;
  32. }
  33.  
  34. void covertToUppercase(char title)
  35. {
  36. title[0] = toupper(title[0]);
  37. cout << title;
  38.  
  39. /*
  40. for (; *sPtr !='\0'; sPtr++)
  41. {
  42. *sPtr = toupper ((unsigned char) *sPtr);
  43. cout << *sPtr;
  44. }
  45. */
  46. /*
  47. while (*sPtr != '\0')
  48. {
  49. if (islower(*sPtr))
  50. *sPtr = toupper((unsigned char)*sPtr);
  51. cout << *sPtr;
  52. sPtr++;
  53. }
  54. */
  55. }

  1. titles.cpp: In function `int main()':
  2. titles.cpp:22: error: invalid conversion from `char*' to `char'
  3. titles.cpp:22: error: initializing argument 1 of `void covertToUppercase(char)'
  4. titles.cpp: In function `void covertToUppercase(char)':
  5. titles.cpp:36: error: invalid types `char[int]' for array subscript
  6. titles.cpp:36: error: invalid types `char[int]' for array subscript
  7.  
<< moderator edit: added [code][/code] tags >>

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.
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,450
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Convert first character from lowercase to uppercase?

 
0
  #10
Apr 30th, 2005
  1. void covertToUppercase(char title);
This is different from...
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
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC