字符串操作
来源:互联网 发布:人工智能股票代码 编辑:程序博客网 时间:2024/05/01 07:32
自己写的,还有一些不全面
#include<stdio.h>#include<stdlib.h>int StrLength(char[]);int Concat(char* T,char s1[],char s2[]);int Index(char S[],char T[],int pos);char *reverse(char*,int);int main(){ char a1[11]="abcdefghijk"; char a2[8]="jkfghlmn";/* int len1=StrLength(a1); int len2=StrLength(a2); printf("a1's length is : %d\n",len1); printf("a2's length is : %d\n",len2);*//* int len1=StrLength(a1); int len2=StrLength(a2); char array[len1+len2]; Concat(array,a1,a2); int i; for(i=0;i<len1+len2;i++) printf("%s",array[i]);*///----------------compare------------------------/* int temp=Strcompare(a1,a2); if(temp>0) printf("a1 is larger than a2\n"); else if(temp < 0) printf("a1 is smaller than a2"); else printf("a1 equals to a2");*/ //----------------Index--------------------------/* int temp=Index(a1,a2,0); if(temp==-1) printf("sorry, the main string doesn't contains the other string\n"); else printf("the matching begins from the %d-th element\n",temp);*/ int i=0; char array[4]="love"; char * p=reverse(array,4); printf("%s",p); return 1;}//-------lenth of a string------------int StrLength(char s[]){ int len=0; while(*s!='\0'){ s+=1; len++; } return len; }int Concat(char* T,char s1[],char s2[]){ int i; int len1=StrLength(s1); int len2=StrLength(s2); for(i=0;i<len1;i++) *(T+i)=s1[i]; for(i=0;i<len2;i++) *(T+len1+i)=s2[i]; return 1;}int Strcompare(char s1[],char s2[]){ int i; int len1=StrLength(s1); int len2=StrLength(s2); for(i=0;i< len1 && i< len2;i++) if(s1[i]!=s2[i]) return s1[i]-s2[i]; return len1-len2;}int Index(char S[],char T[],int pos){//pos starts from 0 int len1=StrLength(S); int len2=StrLength(T); if(pos>len1||(pos+len2)>len1) return -1; int i=pos,j=0; while(i <len1 && j< len2){ if(S[i]==T[j]){++i;++j;} else{i=i-j+1;j=0;} } if(j>=len2) return i-len2+1;//the i-len1+1-th element else return -1;}char * reverse(char *s,int len){ if(len==0) return NULL; char * low=s; char * high=s+len-1; char temp; while(low<high){ temp=*high; *high=*low; *low=temp; low++;high--; } return s;}
0 0
- 字符串操作
- 字符串操作
- 操作字符串
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- python修改logging模块实现日志按天写入
- GridView 按钮
- SpringMVC之实现用户管理
- UVA 1342 That Nice Euler Circuit(二维几何基础)
- CodeForces-71A-Way Too Long Words
- 字符串操作
- MySQL字符串函数
- Android学习之GroupActivity
- BZOJ 1012 JSOI2008 最大数maxnumber 单调栈+二分 / 线段树
- 平衡二叉树
- 将文章写入数据库并取出显示
- 学会使用filter过滤器过滤字符集,以及如何配置web.xml
- Windows下共享内存示例
- c指针面试