| | |
Using string as char array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
How would I delete one char from a string?
C++ Syntax (Toggle Plain Text)
string aWord = "question?"; while(aWord[i]) { if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]); else delete[i]aWord; i++; }
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
•
•
•
•
How would I delete one char from a string?
C++ Syntax (Toggle Plain Text)
string aWord = "question?"; while(aWord[i]) { if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]); else delete[i]aWord; i++; }
http://www.cplusplus.com/reference/s...g/replace.html
C++ Syntax (Toggle Plain Text)
string aWord = "abcdef"; aWord.replace (3, 1, "");
aWord now contains "abcef";Edit: Or just use erase:
C++ Syntax (Toggle Plain Text)
aWord.erase (3, 1);
Has the same effect.
Last edited by VernonDozier; Dec 2nd, 2008 at 3:45 pm.
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
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)
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 3:56 pm.
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
•
•
•
•
How would I delete one char from a string?
C++ Syntax (Toggle Plain Text)
string aWord = "question?"; while(aWord[i]) { if(isalpha(aWord[i]))printf ("character %c is alphabetic\n",aWord[i]); else delete[i]aWord; i++; }
cpp Syntax (Toggle Plain Text)
string strWord = "Hello, World!"; string strResult = ""; for(size_t idx = 0; idx < strWord.size(); ++idx) { if (isalpha(strWord[idx])) { strResult += strWord[idx]; } } cout<< strResult<<endl;
Another Way
cpp Syntax (Toggle Plain Text)
string strWord = "Hello, World! Laiq?"; size_t pos = 0; for(size_t idx = 0; idx < strWord.size(); ++idx) { if (isalpha(strWord[idx])) { strWord[pos] = strWord[idx]; ++pos; } } strWord.resize(pos); cout<< strWord <<endl;
Little more tricky.
cpp Syntax (Toggle Plain Text)
Check the remove_if (...)
Hope the above techniques might help you in understanding ?
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
•
•
•
•
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)
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;
C++ Syntax (Toggle Plain Text)
while (aWord[i])
to
C++ Syntax (Toggle Plain Text)
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.
Check the size..
C++ Syntax (Toggle Plain Text)
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 5:34 pm.
![]() |
Similar Threads
- String to char* conversion (C++)
- How does one tolower an entire string? (C++)
- Split string into char array (C)
- One line of code copy char array using pointers (C++)
- How to read data from csv file in an array and parse (C++)
- String Object Immutable Confusion (Java)
- Problem with string search of an array (C++)
- two-dimensional char array (Java)
- Using .length() with multidimensional char array ? (Help) (C)
- string to integer array transformation (C)
Other Threads in the C++ Forum
- Previous Thread: Finding Positon of characters
- Next Thread: Char array and Getline question - really basic
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






