查找兄弟单词
计算字符个数#include <iostream> #include <string> using namespace std; unsigned int number; int findbrother(string*,string,string*); void swapc(char* a,char* b); int rankletter(string &str); void swaps(string* a,string* b); void rankstring(string *str); int main() { int rank; cin>>number; if (number>1000) { return -1; } string* str=new string[number]; string* s1=new string[number]; for(int i=0;i<number;i++) { cin>>str[i]; } string bro; cin>>bro; cin>>rank; int n=findbrother(str,bro,s1); cout<<n<<endl; rankstring(s1); cout<<s1[rank]; delete[]s1; delete[]str; return 0; } int findbrother(string* str,string bro,string* s1) { string bro1=bro; string strss; int count=0; rankletter(bro1); for (int i=0;i<number;i++) { if (str[i]==bro) { continue; } strss=str[i]; rankletter(strss); if (strss==bro1) { s1[count]=str[i]; count++; } } return count; } int rankletter(string &bro) { int i=0; int k; int n=bro.size(); while (i<n) { for(int j=0;j<n-1-i;j++) { k=j+1; if (k==n) { continue; } if(bro[k]< bro[j]) { swapc(&bro[j],&bro[k]); } } i++; } return 0; } void swapc(char* a,char* b) { char temp; temp=*a; *a=*b; *b=temp; } void rankstring(string* str) { int i=0; int k; while (i<number) { for(int j=0;j<number-1-i;j++) { k=j+1; if (k==number) { continue; } if(str[k]< str[j]) { swaps(&str[j],&str[k]); } } i++; } } void swaps(string* a,string* b) { string temp; temp=*a; *a=*b; *b=temp; }
字符串分隔#include <iostream> #include <string> using namespace std; int main() { int count=0; char ch; char* T=new char[2000]; cin.getline(T,2000); cin>>ch; int n=strlen(T); for (int i=0;i<n;i++) { if (toupper(ch) >='A' && toupper(ch) <= 'Z') { if (toupper(T[i])==toupper(ch)) { count++; } } else { if (T[i]==ch) { count++; } } } cout<<count<<endl; return 0; }
字符串合并处理#include <iostream> #include <string> using namespace std; void AddString(string *str); unsigned int number; int GetLength(); int main() { cin>>number; string* str=new string[number]; getline(cin,str[0]); for (int i=0;i<number;i++) { getline(cin,str[i]); if (str[i].size()==0) { return -1; } } AddString(str); string stryy[5000]; string strss; int count=0; for(int i=0;i<number;i++) { int n=str[i].size(); for (int j=0;j<n/8;j++) { basic_string <char> strss = str[i].substr(j*8,8); stryy[count]=strss; count++; } } for(int i=0;i<count;i++) { cout<<stryy[i]<<endl; } delete[]str; return 0; } void AddString(string *str) { for (int i=0;i<number;i++) { int n=str[i].size(); int a=n/8; int b=8*(a+1); while(n<b) { str[i].push_back('0'); n++; } } }
提取不重复整数#include <iostream> #include <string> void ProcessString(char* str1,char *str2,char * strOutput); using namespace std; const int MAX = 1000; void process(char* ); void swapc(char* a,char* b) { char temp; temp=*a; *a=*b; *b=temp; } int main() { char* str1=new char[MAX]; char* str2=new char[MAX]; char* strOutput=new char[MAX]; cin>>str1>>str2; if (strlen(str1)==0||strlen(str2)==0) { return -1; } ProcessString(str1,str2,strOutput); process(strOutput); cout<<strOutput; delete[]str2; delete[]str1; return 0; } void ProcessString(char* str1,char *str2,char* strOutput) { strcat(str1,str2); strcpy(strOutput,str1); int n=strlen(strOutput); for (int k=0;k<n;k+=2) { for (int i=0;i<n-k-2;i+=2) { int j=i+2; if(strOutput[j]<strOutput[i]) { swapc(&strOutput[i],&strOutput[j]); } } for (int i=1;i<n-k-2;i+=2) { int j=i+2; if(strOutput[j]<strOutput[i]) { swapc(&strOutput[i],&strOutput[j]); } } } } void process(char* strOutput) { char c; int n=strlen(strOutput); for(int j=0;j<n;j++) { c=strOutput[j]; switch (c) { case '0': strOutput[j]='0'; continue; case '1': strOutput[j]='8'; continue; case '2': strOutput[j]='4'; continue; case '3': strOutput[j]='C'; continue; case '4': strOutput[j]='2'; continue; case '5': strOutput[j]='A'; continue; case '6': strOutput[j]='6'; continue; case '7': strOutput[j]='E'; continue; case '8': strOutput[j]='1'; continue; case '9': strOutput[j]='9'; continue; case 'a': case 'A': strOutput[j]='5'; continue; case 'b': case 'B': strOutput[j]='D'; continue; case 'c': case 'C': strOutput[j]='3'; continue; case 'd': case 'D': strOutput[j]='B'; continue; case 'e': case 'E': strOutput[j]='7'; continue; case 'f': case 'F': strOutput[j]='F'; continue; default : continue; } } }
字符串反转#include <iostream> #include <string> using namespace std; int findone(char* str,char* strss); int main() { char* str=new char[1000]; char* strss=new char[1000]; int t=0; cin.getline(str,1000); int n=findone(str,strss); if (strss[0] == '0') printf("%d", strtol(strss + 1, NULL, 10)); else printf("%d", strtol(strss, NULL, 10)); delete[]strss; delete[]str; return 0; } int findone(char* str,char* strss) { int n=strlen(str); int i=n-2; int k=1; strss[0]=str[n-1]; int flag=0; while(i>=0) { flag=0; for (int j=0;j<k;j++) { if(str[i]==strss[j]) { flag=1; break; } } if (flag==0) { strss[k]=str[i]; k++; } i--; } strss[k]='\0'; return k; }
字符串匹配#include <iostream> #include <string> using namespace std; void reversedstring(char *str); void swapc(char* a,char* b); unsigned int MAX=5000; int main() { char* str=new char[MAX]; cin.getline(str,MAX); if (str==NULL) { return -1; } reversedstring(str); cout<<str<<endl; return 0; } void reversedstring(char *str) { int n=strlen(str); for(int j=0;j<n/2;j++) { swapc(&str[j],&str[n-1-j]); } } void swapc(char* a,char* b) { char temp; temp=*a; *a=*b; *b=temp; }
#include <iostream> #include <string> using namespace std; int main() { string T;//模式 string P;//原串 getline(cin,P); getline(cin,T); int count=0; int begin=-1; while((begin=T.find(P,begin+1))!=string::npos) { count++; } if(count==1) cout<<"true"<<endl; else cout<<"false"<<endl; return 0; }