字符串操作

11 篇文章 0 订阅
2 篇文章 0 订阅


自己写的,还有一些不全面

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值