Using string as char array

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

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Using string as char array

 
0
  #1
Dec 2nd, 2008
How would I delete one char from a string?

  1. string aWord = "question?";
  2.  
  3. while(aWord[i])
  4. {
  5. if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
  6. else
  7. delete[i]aWord;
  8. i++;
  9. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Using string as char array

 
0
  #2
Dec 2nd, 2008
Originally Posted by JackDurden View Post
How would I delete one char from a string?

  1. string aWord = "question?";
  2.  
  3. while(aWord[i])
  4. {
  5. if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
  6. else
  7. delete[i]aWord;
  8. i++;
  9. }
Use the replace function from string:

http://www.cplusplus.com/reference/s...g/replace.html

  1. string aWord = "abcdef";
  2. aWord.replace (3, 1, "");

aWord now contains "abcef";

Edit: Or just use erase:
  1. aWord.erase (3, 1);

Has the same effect.
Last edited by VernonDozier; Dec 2nd, 2008 at 3:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Using string as char array

 
0
  #3
Dec 2nd, 2008
Note you can use find() or find_first_of() etc to get the index of the character youare after.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Using string as char array

 
0
  #4
Dec 2nd, 2008
Well that sort of works but if the char I want to delete/erase is at the end of the string my compiler crashes. I dont understand how to delete/erase a char that is not an alphanumeric type.

  1. int i=0;
  2. string aWord = "abcdf!";
  3.  
  4. while(aWord[i])
  5. {
  6. if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
  7. else
  8. aWord.erase (i, 1);
  9. i++;
  10. }
  11. cout<<aWord;
Last edited by JackDurden; Dec 2nd, 2008 at 3:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Using string as char array

 
0
  #5
Dec 2nd, 2008
Originally Posted by JackDurden View Post
How would I delete one char from a string?

  1. string aWord = "question?";
  2.  
  3. while(aWord[i])
  4. {
  5. if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
  6. else
  7. delete[i]aWord;
  8. i++;
  9. }
I can give you two ways. one through STL & other through using the strings only

  1. string strWord = "Hello, World!";
  2. string strResult = "";
  3.  
  4. for(size_t idx = 0; idx < strWord.size(); ++idx) {
  5. if (isalpha(strWord[idx])) {
  6. strResult += strWord[idx];
  7. }
  8. }
  9. cout<< strResult<<endl;

Another Way
  1. string strWord = "Hello, World! Laiq?";
  2. size_t pos = 0;
  3.  
  4. for(size_t idx = 0; idx < strWord.size(); ++idx) {
  5. if (isalpha(strWord[idx])) {
  6. strWord[pos] = strWord[idx];
  7. ++pos;
  8. }
  9. }
  10. strWord.resize(pos);
  11.  
  12. cout<< strWord <<endl;

Little more tricky.
  1. Check the remove_if (...)

Hope the above techniques might help you in understanding ?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Using string as char array

 
0
  #6
Dec 2nd, 2008
Originally Posted by JackDurden View Post
Well that sort of works but if the char I want to delete/erase is at the end of the string my compiler crashes. I dont understand how to delete/erase a char that is not an alphanumeric type.

  1. int i=0;
  2. string aWord = "abcdf!";
  3.  
  4. while(aWord[i])
  5. {
  6. if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
  7. else
  8. aWord.erase (i, 1);
  9. i++;
  10. }
  11. cout<<aWord;
Your program ran for me. I don't know what error you got, but if it is an index-out-of-range/segmentation fault error, you may want to change this:
  1. while (aWord[i])

to

  1. while (i < aWord.length ())

That might make a difference, might not. As to your other problem, only increment i if aWord[i] is a letter. erase and replace will cause the indexes to change, so some characters will be skipped if you use erase/replace AND increment i.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Using string as char array

 
0
  #7
Dec 2nd, 2008
Check the size..
  1.  
  2. int i=0;
  3. string aWord = "abcdf!";
  4.  
  5. while(aWord[i])
  6. {
  7. if(isalpha(aWord[i]))
  8. printf ("character %c is alphabetic\n",aWord[i]);
  9. else
  10. {
  11. aWord.erase (i, 1);
  12. //..Is idx > length
  13. if(i>aWord.size())
  14. break;
  15. i--;
  16. }
  17. i++;
  18. }
  19. cout<<aWord;
Last edited by cikara21; Dec 2nd, 2008 at 5:34 pm.
.:-cikara21-:.
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