Question #70675

consider a string of numbers, slashes, spaces, and miscellaneous character.
Say for example, "4!@/#@$6@#$ 7"
I want to extract numbers and slashes in the given format : a/b c
How do I extract only "4/6 7" from the above string in c++?

Expert's answer

#include <iostream>
#include <string>

using namespace std;

int main()
{
string s= "4!@/#@$6@#$ 7", result="";
//"4/6 7"
for(int i=0;i<s.size();i++)
if(s[i]>='0' && s[i]<='9'|| s[i]==' ' || s[i]=='/')
result+=s[i];
cout << "Result: " << result<<endl;

return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS