View Single Post
Join Date: Jan 2008
Posts: 3,831
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