943,844 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 902
  • C++ RSS
Dec 2nd, 2008
0

Using string as char array

Expand Post »
How would I delete one char from a string?

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Dec 2nd, 2008
0

Re: Using string as char array

Click to Expand / Collapse  Quote originally posted by JackDurden ...
How would I delete one char from a string?

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  1. string aWord = "abcdef";
  2. aWord.replace (3, 1, "");

aWord now contains "abcef";

Edit: Or just use erase:
C++ Syntax (Toggle Plain Text)
  1. aWord.erase (3, 1);

Has the same effect.
Last edited by VernonDozier; Dec 2nd, 2008 at 3:45 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Dec 2nd, 2008
0

Re: Using string as char array

Note you can use find() or find_first_of() etc to get the index of the character youare after.

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 2nd, 2008
0

Re: Using string as char array

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Dec 2nd, 2008
0

Re: Using string as char array

Click to Expand / Collapse  Quote originally posted by JackDurden ...
How would I delete one char from a string?

C++ Syntax (Toggle Plain Text)
  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

cpp Syntax (Toggle Plain Text)
  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
cpp Syntax (Toggle Plain Text)
  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.
cpp Syntax (Toggle Plain Text)
  1. Check the remove_if (...)

Hope the above techniques might help you in understanding ?
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Dec 2nd, 2008
0

Re: Using string as char array

Click to Expand / Collapse  Quote originally posted by JackDurden ...
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.

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. while (aWord[i])

to

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Dec 2nd, 2008
0

Re: Using string as char array

Check the size..
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Finding Positon of characters
Next Thread in C++ Forum Timeline: Char array and Getline question - really basic





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC