-
C++ (
http://www.daniweb.com/forums/forum8.html)
| JackDurden | Dec 2nd, 2008 3:29 pm | |
| Using string as char array 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++;
} |
| VernonDozier | Dec 2nd, 2008 3:37 pm | |
| Re: Using string as char array Quote: Originally Posted by JackDurden (Post 748953) 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. |
| Freaky_Chris | Dec 2nd, 2008 3:45 pm | |
| 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 |
| JackDurden | Dec 2nd, 2008 3:55 pm | |
| 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.
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; |
| Laiq Ahmed | Dec 2nd, 2008 4:02 pm | |
| Re: Using string as char array Quote: Originally Posted by JackDurden (Post 748953) 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 ? |
| VernonDozier | Dec 2nd, 2008 4:42 pm | |
| Re: Using string as char array Quote: Originally Posted by JackDurden (Post 748969) 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. |
| cikara21 | Dec 2nd, 2008 5:22 pm | |
| Re: Using string as char array 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; |
| All times are GMT -4. The time now is 11:22 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC