Using string as char array
Please support our C++ advertiser: Programming Forums
![]() |
•
•
Posts: 87
Reputation:
Solved Threads: 0
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++;
}•
•
Posts: 2,896
Reputation:
Solved Threads: 366
•
•
•
•
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.
•
•
Posts: 87
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.
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.
•
•
Posts: 110
Reputation:
Solved Threads: 13
•
•
•
•
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
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 ?
•
•
Posts: 2,896
Reputation:
Solved Threads: 366
•
•
•
•
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Split string into char array (C)
- One line of code copy char array using pointers (C++)
- How does one tolower an entire string? (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
•
•
•
•
Views: 531 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode