charming2440
全部博文(190)
2014年(9)
2011年(32)
2010年(149)
dzminglo
nasca
戰神神戰
tomcodin
wayneshe
cool138
听风_12
godslion
Kuangjay
REFRESHM
分类: C/C++
2010-10-15 10:29:09
//define the struct of the string typedef struct { char *str; int length; }string; //initlias the string bool strassign(string &s, char *chars) { int i,j; char *c; for (i = 0,c = chars;*c;i++,c++) ; if (!i) { s.str = NULL; s.length = 0; } else { if (!(s.str = (char*)malloc(i*sizeof(char)))) return false; for (j = 0; j < i; j++) { s.str[j] = chars[j]; } s.length = i; } return true; } //find the sub string bool substring(string s, string &substring, int pos, int len) { int i; if (pos < 0 || pos > s.length - 1 || len < 0 || len > s.length - pos) return false; if (sub.str) { free(sub.str); } if (!len) { sub.str = NULL; sub.length = 0; } else { if (!(sub.str = (char*)malloc(sizeof(char)*len))) return false; for (i = 0; i < len; i++) { sub.str[i] = s.str[pos + i]; } sub.length = len } return true; } //positioning the substring bool index(string s, string t, int &pos) { int i,j; i = 0; j = 0; while (i < s.length && j < t.length) { if (s.str[i] == t.str[i]) { i++; j++; } else { i = i-j+1; j = 0; } } if (j == t.length) { pos = i - t.length; return true; } else return false; } //copy the string bool strcpy(string &s,string t) { int i; if (!s.str) { free(s.str); } if (!t.length) { s.str = NULL; s.length = 0; } for (i = 0; i < t.length; i ++) { s.str[i] = t.str[i]; } s.length = t.length; return true; } //insert the sub string bool insert(string &s, string t , int pos) { int i; if (pos <0 || pos > s.length) return false; if (t.str) { if (!(s.str = (char*)realloc(s.str,(s.length + t.length) * \ sizeof(char)))) return false; if (i = s.length; i >= pos; i--) { t.str[i+t.length] = t.str[i]; } for (i = o; i < t.length; i++) { s.str[pos+i] = t.str[i]; } s.length = s.length + t.length; } return true; }
上一篇:华为面试sort
下一篇:SPI通信原理及通信协议
chinaunix网友2010-10-15 16:57:44
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com
登录 注册