944,137 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 21608
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 28th, 2005
0

Re: Convert first character from lowercase to uppercase?

Expand Post »
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. >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Quickslvr is offline Offline
5 posts
since Apr 2005
Apr 28th, 2005
0

Re: Convert first character from lowercase to uppercase?

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'
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 29th, 2005
0

Re: Convert first character from lowercase to uppercase?

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!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Quickslvr is offline Offline
5 posts
since Apr 2005
Apr 29th, 2005
0

Re: Convert first character from lowercase to uppercase?

>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. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 29th, 2005
0

Re: Convert first character from lowercase to uppercase?

>*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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 29th, 2005
0

Re: Convert first character from lowercase to uppercase?

Quote 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
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 29th, 2005
0

Re: Convert first character from lowercase to uppercase?

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Quickslvr is offline Offline
5 posts
since Apr 2005
Apr 29th, 2005
0

Re: Convert first character from lowercase to uppercase?

>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 );
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 30th, 2005
0

Re: Convert first character from lowercase to uppercase?

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Quickslvr is offline Offline
5 posts
since Apr 2005
Apr 30th, 2005
0

Re: Convert first character from lowercase to uppercase?

  1. void covertToUppercase(char title);
This is different from...
void covertToUppercase(char *title);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: Counting the Number of Lines in File
Next Thread in C Forum Timeline: how to create a vector of type structure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC