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++;
}
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++;
}
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/string/string/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.
Note you can use find() or find_first_of() etc to get the index of the character youare after.
Chris
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;
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
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
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.
Check the remove_if (...)
Hope the above techniques might help you in understanding ?
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 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..
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;
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.