RSS Forums RSS

Using string as char array

Please support our C++ advertiser: Programming Forums
Reply
Posts: 87
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

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

string aWord = "question?";

while(aWord[i])
	{
	if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
	else
		delete[i]aWord;
    i++;
  }
AddThis Social Bookmark Button
Reply With Quote  
Posts: 2,896
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 366
VernonDozier VernonDozier is offline Offline
Posting Maven

Re: Using string as char array

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

string aWord = "question?";

while(aWord[i])
	{
	if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
	else
		delete[i]aWord;
    i++;
  }


Use the replace function from string:

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

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

aWord now contains "abcef";

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

Has the same effect.
Last edited by VernonDozier : Dec 2nd, 2008 at 2:45 pm.
Reply With Quote  
Posts: 665
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: 107
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Using string as char array

  #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  
Posts: 87
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

  #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.

  int i=0;
 string aWord = "abcdf!";

while(aWord[i])
	{
	if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
	else
		aWord.erase (i, 1);
    i++;
  }
  cout<<aWord;
Last edited by JackDurden : Dec 2nd, 2008 at 2:56 pm.
Reply With Quote  
Posts: 110
Reputation: Laiq Ahmed is on a distinguished road 
Solved Threads: 13
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Using string as char array

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

string aWord = "question?";

while(aWord[i])
	{
	if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
	else
		delete[i]aWord;
    i++;
  }


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  
Posts: 2,896
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 366
VernonDozier VernonDozier is offline Offline
Posting Maven

Re: Using string as char array

  #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.

  int i=0;
 string aWord = "abcdf!";

while(aWord[i])
	{
	if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]);
	else
		aWord.erase (i, 1);
    i++;
  }
  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:
while (aWord[i])

to

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  
Posts: 314
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 61
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Using string as char array

  #7  
Dec 2nd, 2008
Check the size..
  
int i=0;
string aWord = "abcdf!";

while(aWord[i])
{
   if(isalpha(aWord[i]))
      printf ("character %c is alphabetic\n",aWord[i]);
   else
   {
	aWord.erase (i, 1);
      //..Is idx > length
       if(i>aWord.size())
          break;
       i--;
   }
   i++;
}
cout<<aWord;
Last edited by cikara21 : Dec 2nd, 2008 at 4:34 pm.
.:-cikara21-:.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 531 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:57 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC