标签:
//顺序串 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> using namespace std; #define OK 1 #define FALSE 0 #define ERROR -1 #define MAXSTRLEN 40 typedef int Status; typedef char String [MAXSTRLEN + 1]; //生成一个值等于S的串T Status StrAssign(String T,char *S){ int i; if(strlen(S) > MAXSTRLEN) return ERROR; T[0] = strlen(S); for(i = 1; i <= T[0]; i++) T[i] = *(S + i - 1); return OK; } //由S复制得T Status StrCopy(String T,String S){ int i; for(i = 0; i <= S[0]; i++) T[i] = S[i]; return OK; } //判断S是否为空 Status StrEmpty(String S){ if(S[0] == 0) return true; else return false; } //比较两串 Status StrCompare(String S,String T){ int i; for(i = 1; i <= S[0] && i <= T[0]; i++){ if(S[i] != T[i]) return S[i] - T[i]; } return 0; } int StrLength(String S){ return S[0]; } Status ClearString(String S){ S[0] = 0; return OK; } Status Concat(String T,String S1,String S2){ int i,j; if(S1[0] + S2[0] <= MAXSTRLEN){ for(i = 1; i<= S1[0]; i++){ T[i] = S1[i]; } for(j = 1; j <= S2[0]; j++){ T[j + S1[0]] = S2[j]; } T[0] = S1[0] + S2[0]; return true; } else{ for(i = 1; i<= S1[0]; i++){ T[i] = S1[i]; } for(i = 1; i <= MAXSTRLEN - S1[0]; i++){ T[i + S1[0]] = S2[i]; } T[0] = MAXSTRLEN; return false; } } //用Sub返回串S的第pos个字符起长度为len的子串。 Status SubString(String Sub,String S,int pos,int len){ int i; if(pos < 1 || pos > S[0] || len < 0 || len > S[0] - pos + 1) return ERROR; for(i = 1; i <= len; i++){ Sub[i] = S[pos + i - 1]; } Sub[0] = len; return OK; } //返回子串T在主串S中第pos个字符之后的位置,若不存在,则函数值为0。 int Index(String S,String T,int pos){ int i,j; if(pos >= 1 && pos <= S[0]){ i = pos; j = 1; while(i <= S[0] && j <= T[0]){ if(S[i] == T[j]){ ++i; ++j; } else{ i = i - j + 2; j = 1; } } if(j > T[0]) return i - T[0]; else return 0; } else return 0; } //在串S的第pos个字符之前插入串T。完全插入返回TRUE,部分插入返回FALSE Status StrInsert(String S,int pos,String T){ int i; if(pos < 1 || pos > S[0] + 1) return ERROR; if(S[0] + T[0] <= MAXSTRLEN){ for(i = S[0]; i >= pos; i--){ S[i + T[0]] = S[i]; } for(i = pos; i < pos + T[0]; i++){ S[i] = T[i - pos + 1]; } S[0] = S[0] + T[0]; return true; } else{ for(i = MAXSTRLEN; i <= pos; i--){ S[i] = S[i - T[0]]; } for(i = pos; i <pos + T[0]; i++){ S[i] = T[i - pos + 1]; } S[0] = MAXSTRLEN; return false; } } Status StrDelete(String S,int pos,int len){ int i; if(pos < 1 || pos > S[0] - len + 1 || len < 0) return ERROR; for(i = pos + len; i <= S[0]; i++){ S[i - len] = S[i]; } S[0] -= len; return OK; } //用V替换主串S中出现的所有与T相等的不重叠的子串 Status Replace(String S,String T,String V){ int i = 1; if(StrEmpty(T)) return ERROR; do{ i = Index(S,T,i); if(i){ StrDelete(S,i,StrLength(T)); StrInsert(S,i,V); i += StrLength(V); } }while(i); return OK; } //S2为S1的逆序串 void reversal(String S1,String &S2) { int n = S1[0]; S2[0] = S1[0]; for(int i = 1; i <= n; i++) { S2[i] = S1[n - i + 1]; } } void StrPrint(String T){ int i; for(i = 1; i <= T[0]; i++){ printf("%c",T[i]); } printf("\n"); } int main() { String c; String a,b; scanf("%s",c); StrAssign(a,c); StrPrint(a); reversal(a,b); StrPrint(b); return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/5002504.html