题目:把给定的字符串以单词的方式反序,其中,给定的子串不倒转。
比如:s[] = "The quick brown fox jumps over the lazy dog"; 子串: sub[] = "brown fox";
结果应该是:dog lazy the over jumps brown fox quick The.
- #include<iostream>
- using namespace std;
- void reverse(char* s);
- void func(char* s, char* sub);
- int main()
- {
- char s[] = "The quick brown fox jumps over the lazy dog";
- char sub[] = "The quick";
- func(s, sub);
- system("pause");
- return 0;
- }
- void reverse(char* s, char * s1)
- {
- int n = strlen(s)-1;
- for(int i=n; i>=0; i--)
- {
- if(s[i]==' ')
- {
- int temp = i+1;
- strcat(s1, s+temp);
- strcat(s1, " ");
- s[i] = '/0';
- }
- }
- strcat(s1, s);
- strcat(s1, "/0");
- }
- void func(char* s, char* sub)
- {
- int sizeof_s = strlen(s)+1;
- char* s1 = new char[sizeof_s];
- s1[0] = '/0';
- char* s_0 = new char[strlen(s)+1];
- strcpy(s_0, s);
- reverse(s_0, s1);
- cout << "s1 = " << s1 << endl;
- int sizeof_sub = sizeof(sub)+1;
- char* sub1 = new char[sizeof_sub];
- sub1[0] = '/0';
- char* sub_0 = new char[strlen(sub)+1];
- strcpy(sub_0, sub);
- reverse(sub_0, sub1);
- cout << "sub1 = " << sub1 << endl;
- int s_length = strlen(s1);
- int sub_length = strlen(sub1);
- for(int i=0; i<s_length; i++)
- {
- if(s1[i]==sub1[0] && s1[i]!=' ')
- {
- for(int j=1; j<strlen(sub1); j++)
- {
- if(s1[i+j]!=sub1[j])
- {
- break;
- }
- cout << sub ;
- i = i+sub_length;
- }
- }
- cout << s1[i] ;
- }
- delete[] s1;
- delete[] s_0;
- delete[] sub1;
- delete[] sub_0;
- }