Replace \ delete character in string

string DelStr = "I! am! bored!"; string RepStr = "10/07/10" 

I want to delete everything '!' to DelStr, and I want to replace all the "/" with "-" in the RepStr line.

Is there a way to do this without looping through each character?

+7
c ++ string
source share
2 answers

Remove exclamations:

 #include <algorithm> #include <iterator> std::string result; std::remove_copy(delStr.begin(), delStr.end(), std::back_inserter(result), '!'); 

Alternatively, if you want to print a line, you do not need the result variable:

 #include <iostream> std::remove_copy(delStr.begin(), delStr.end(), std::ostream_iterator<char>(std::cout), '!'); 

Replace features with dashes:

 std::replace(repStr.begin(), repStr.end(), '/', '-'); 
+12
source share
 #include<iostream.h> #include<string.h> #include<conio.h> void main() { clrscr(); char a[200],ch,ch1; int temp=0,i,j,x,len,z,f,k=0; cout<<"Enter String: "; cin.getline(a,150); len=strlen(a); cout<<"\n\nLength Of String: "; cout<<len; cout<<"\n\n\nReplace: "; cin>>ch; cout<<"\n\nReplace with: "; cin>>ch1; for(i=0;i<len;i++) { if(ch==a[i]) { temp=a[i]; a[i]=ch1; } } cout<<"\n\nUpdated String: "; for(i=0;i<len;i++) { cout<<a[i]; } getch(); } Example: Enter String: Hey! How Are You. Replace: H Replace with: m Output: mey! mow Are You. (Note: Every character has its ascii code. Such as 'H' and 'h' are two different characters.) 
0
source share

All Articles