codeGeek
BAN USERAlgo: regex_matcher(s1, s2)
1. if s1 and s2 are empty return true
2. if *s1 and *s2 are same recurse for rest of the strings
3. if in wildcard string(s2) we find * we have option of skipping it and going ahead with next character or match one character in s1 and recurse
4. if none of the above matches return false
#include<iostream>
using namespace std;
bool regex_matcher(char *s1, char *s2)
{
if(!*s1 && !*s2)
return true;
if(*s2 == '*' && *(s2+1) != '\0' && *s1 == '\0')
return false;
if(*s1 == *s2)
return regex_matcher(s1+1, s2+1);
if(*s2 == '*')
return (regex_matcher(s1, s2+1) || regex_matcher(s1+1, s2));
return false;
}
int main()
{
bool matched = regex_matcher("index.html", "i*************x.htm*");
if(matched)
cout<<"matched"<<endl;
else
cout<<"not matched"<<endl;
return 0;
}
#include<iostream>
using namespace std;
//s=12345678
//s[3],s[4]
//s[2],s[3] s[4],s[5]
//s[1],s[2] s[3],s[4] s[5],s[6]
void rearrange(string &str)
{
int l = str.length();
int center = (l/2)-1;
int j, m, n;
m = center;
n = 0;
for(int i = 1; i<=center; i++)
{
j = 0;
while(j<i)
{
char t = str[m];
str[m] = str[m+1];
str[m+1] = t;
j++;
m = m+2;
}
m = center - i;
}
}
int main()
{
string str = "abcdefghijklmn";
cout<<str<<endl;
rearrange(str);
cout<<str<<endl;
return 0;
}
- codeGeek January 17, 2014
- codeGeek January 24, 2014