how to remove string from string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to remove string from string?

example input 102030 output 123 input 10009 output 19 //string without space like the example

4th Nov 2018, 3:52 PM
Yoaraci
Yoaraci - avatar
3 Answers
+ 14
#include <iostream> #include<cstring> using namespace std; int main() { string str="1030012011"; //sample string string res=""; //result string to be printed for(int i=0;i<str.length();i++) if(str[i]!='0') { res+=str[i]; } cout<<res<<endl; return 0; } //I am writing simple logic followed here : 1)make a string variable to print result 2)run a loop through each alphabet of string (though here its numbers) 3)whenever the alphabet is not '0' , then add that alphabet to res 4)print res
4th Nov 2018, 4:46 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 5
Oh😕yes I seeing,
4th Nov 2018, 4:52 PM
Izaak GOLDSTEIN
Izaak GOLDSTEIN - avatar
+ 4
You can do it in two ways Removing from original string. This one you will do it more operations but spend less memory and you will lose original input. Or Removing using a empty auxiliary array adding the elements that no match your char. Like this: https://code.sololearn.com/cDxMj07PCV6R/?ref=app In this case you do less operations but this costs more memory and you don't lose original input.
4th Nov 2018, 4:55 PM
Anya
Anya - avatar