| | |
Using string as char array
![]() |
•
•
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,749
Reputation:
Solved Threads: 491
•
•
•
•
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,749
Reputation:
Solved Threads: 491
•
•
•
•
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
- How does one tolower an entire string? (C++)
- Split string into char array (C)
- One line of code copy char array using pointers (C++)
- String to char* conversion (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 based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker listing loop looping loops map math matrix memory multiple news node output pointer port problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






