View Single Post
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