943,882 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8401
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 1st, 2007
0

Re: Concatenation of strings without string.h

This function will convert the entire sentence to all upper-case. Hope that's what you want to do.
I just wanted to change the first character in the sentence to uppercase, what would I change to do that?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
weasel7711 is offline Offline
82 posts
since Oct 2007
Nov 1st, 2007
0

Re: Concatenation of strings without string.h

>I just wanted to change the first character in the sentence to
>uppercase, what would I change to do that?
Don't call toupper on every character, just on the first one:
C++ Syntax (Toggle Plain Text)
  1. void printSentence(char *sentPtr){
  2. char *sent = sentPtr;
  3.  
  4. *sent = toupper((unsigned char)*sent);
  5.  
  6. while(*sent)
  7. {
  8. ++sent;
  9. }
  10.  
  11. *sent++ = '.';
  12. *sent = 0;
  13. cout << sentPtr << endl;
  14. }
Last edited by Ptolemy; Nov 1st, 2007 at 3:31 pm.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Nov 6th, 2007
0

Re: Concatenation of strings without string.h

Click to Expand / Collapse  Quote originally posted by Ptolemy ...
Don't call toupper on every character, just on the first one:
C++ Syntax (Toggle Plain Text)
  1. *sent = toupper((unsigned char)*sent);
I'm slightly confused as to why the unsigned char makes a difference?


On a side note: I spoke to my professor last week and apparently we are able to use strcat, so I started using that and it fixed the program and its been submitted, however i'd still like to know how to concatenate using my own function just for personal use and education.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
weasel7711 is offline Offline
82 posts
since Oct 2007
Nov 6th, 2007
0

Re: Concatenation of strings without string.h

>I'm slightly confused as to why the unsigned char makes a difference?
toupper takes an integer parameter, but that integer is required to be either EOF or in the range of unsigned char. Because sent holds values that you can't be sure fit in that range, a cast forces the range to avoid undefined behavior.

>however i'd still like to know how to concatenate using
>my own function just for personal use and education.
Here's how one might implement strcat:
C++ Syntax (Toggle Plain Text)
  1. char *strcat ( char * dst, const char * src )
  2. {
  3. char *save = dst;
  4.  
  5. while ( *dst != '\0' )
  6. ++dst;
  7.  
  8. while ( ( *dst++ = *src++ ) != '\0' )
  9. ;
  10.  
  11. return save;
  12. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 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.
Message:
Previous Thread in C++ Forum Timeline: listview groups
Next Thread in C++ Forum Timeline: c++ project





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


Follow us on Twitter


© 2011 DaniWeb® LLC