Concatenation of strings without string.h

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 52
Reputation: weasel7711 is an unknown quantity at this point 
Solved Threads: 0
weasel7711 weasel7711 is offline Offline
Junior Poster in Training

Re: Concatenation of strings without string.h

 
0
  #11
Nov 1st, 2007
Originally Posted by Ancient Dragon View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Concatenation of strings without string.h

 
0
  #12
Nov 1st, 2007
>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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 52
Reputation: weasel7711 is an unknown quantity at this point 
Solved Threads: 0
weasel7711 weasel7711 is offline Offline
Junior Poster in Training

Re: Concatenation of strings without string.h

 
0
  #13
Nov 6th, 2007
Originally Posted by Ptolemy View Post
Don't call toupper on every character, just on the first one:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
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: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Concatenation of strings without string.h

 
0
  #14
Nov 6th, 2007
>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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC