How to remove special chracters from a string

Aug 9, 2012 at 9:14am
Hi all,

I want to remove special characters from a string. eg.

string str="Hell^o, I'am Havi'n Good_Day, toda!y";

should change to

Hello Iam Havin GoodDay today


i can use brute force and check every element but is there any efficient way? I don't want to use regex.

Also i am taking input from file, thus if there's way to ignore special characters whlle input, it'll help too.

thanks!
Aug 9, 2012 at 9:26am
str.resize(remove_if(str.begin(), str.end(),[](char x){return !isalnum(x) && !isspace(x);})-str.begin());
Aug 9, 2012 at 9:51am
this is giving errors


assignment-1.cpp: In function ‘int main()’:
assignment-1.cpp:55:20: error: expected ‘)’ before ‘if’
assignment-1.cpp:55:108: error: invalid conversion from ‘int (*)(const char*)throw ()’ to ‘long unsigned int’
assignment-1.cpp:55:108: error:   initializing argument 1 of ‘void std::basic_string<_CharT, _Traits, _Alloc>::resize(std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
Aug 9, 2012 at 9:55am
there is a '_' between the 'remove' and the 'if'!
Aug 9, 2012 at 2:29pm
I am sorry to bother you again, but following error occurs, Do i need to update libraries? or other way to compile?

i included <algorithm>
and using gcc 4.5.2


assignment-1.cpp:57:93: warning: lambda expressions only available with -std=c++0x or -std=gnu++0x
assignment-1.cpp:57:94: error: no matching function for call to ‘remove_if(std::basic_string<char>::iterator, std::basic_string<char>::iterator, main()::<lambda(char)>)’
Aug 9, 2012 at 2:31pm
You need to enable C++11 features by passing the flag -std=c++0x to the compiler.
Aug 9, 2012 at 2:31pm
Yes, an other way to compile. Add the compiler option "-std=c++0x", as it say in the warning.
Last edited on Aug 9, 2012 at 2:32pm
Aug 9, 2012 at 2:32pm
here's the code for convenience

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<cstdlib>
#include<string>
#include<stack>
#include<omp.h>
#include<fstream>
#include<vector>
#include<algorithm>

using namespace std;

string RemoveSpecials(string str)
{
	int i=0,len=str.length();
	while(i<len)
	{
		char c=str[i];
		if(((c>='0')&&(c<='9'))||((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z'))) 
		{
			if((c>='A')&&(c<='Z')) str[i]+=32; //Assuming dictionary contains small letters only.
			++i;
		}
		else
		{
			str.erase(i,1);
			--len;
		}
	}
	return str;
}

int main()
{
	ifstream dic("dic.txt"),list("list.txt");
	if(dic.is_open()&&list.is_open())
	{
		int dic_l=0,list_l=0,flag=0;

		vector<string> dic_t;
		vector<string> list_t;
		string temp;


		while(!dic.eof()) 
		{
			temp.clear();
			getline(dic,temp);
			dic_t.push_back(temp);
			dic_l++;
		}
		while(!list.eof())
		{
			temp.clear();
			//getline(list,temp);
			list>>temp;
			//temp=RemoveSpecials(temp);
			temp.resize(remove_if(temp.begin(), temp.end(),[](char x){return !isalnum(x) && !isspace(x);})-temp.begin()); 
			list_t.push_back(temp);
			list_l++;
		}

		list_l--;
		dic_l--;

		cout<<dic_l<<" "<<list_l<<endl;

#pragma omp parallel for shared(dic_t,list_t,list_l,dic_l) private(flag) num_threads(4)
		for(int i=0;i<list_l;i++)
		{
			flag=0;
			for(int j=0;j<dic_l;j++)
			{
				if(list_t[i].compare(dic_t[j])==0) 
				{
					flag=1;
					break;
				}
			}
#pragma omp critical
			{
				if(flag==0) cout<<"No! "<<i+1<<": \033[1;4;31m"<<list_t[i]<<"\033[0m does not exists in Dictionary."<<endl;
			}
		}




	}
	else
	{
		cout<<"Cannot find files 'list.txt' and 'dic.txt' !!!"<<endl;
	}


}
Aug 9, 2012 at 2:35pm
Which IDE are you using?
Aug 9, 2012 at 2:36pm
BTW Trust me, that function is WAY too complex. I told you, that one line is enough, you just have to pass "-std=c++0x" to you compiler!
Aug 9, 2012 at 2:42pm
Thank You Very much worked like a charm !!!

Does this syntax used to compile "boost::regex" libraries too?

Does using libraries like algorithm and regex are good sign?
Aug 9, 2012 at 2:52pm
Well, not boost::regex, but it has std::regex. But it's not very good...
Topic archived. No new replies allowed.