字符串常用操作——C语言版

C语言不比Python,它没有丰富的字符串操作函数,也没有像C++那样的String类型的数据。本文就来总结字符串的一些常用操作。

//从键盘输入一段英文,统计小写字母的数量并按数量的降序输出字母
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main()
{
    char word[100];
    int abc[26] = {0},i,j,len_word,loc,val;
    gets(word);
    len_word = strlen(word);
    for(i=0;i<len_word;i++)
    {
        if(islower(word[i]))
        {
            abc[word[i]-'a']++;
        }
    }
    for(i=0;i<26;i++)
    {
        loc = i;
        val = abc[loc];
        for(j=0;j<26;j++)
        {
            if(abc[j]>val)
            {
                loc = j;
                val = abc[j];
            }
        }
        if(val>0)
        {
            printf("%c ",loc+'a');
        }
        abc[loc] = 0;
    }
    return 0;
}
//从键盘输入一段英文,统计小写字母的数量并按数量的升序输出字母
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main()
{
    char word[100];
    int abc[26] = {0},i,j,len_word,loc,val;
    gets(word);
    len_word = strlen(word);
    for(i=0;i<len_word;i++)
    {
        if(islower(word[i]))
        {
            abc[word[i]-'a']++;
        }
    }
    for(i=0;i<26;i++)
    {
        val = 0x7fffffff;
        loc = 26;
        for(j=0;j<26;j++)
        {
            if(abc[j]>0 && abc[j]<=val)
            {
                val = abc[j];
                loc = j;
            }
        }
        if(val>0 && val<0x7fffffff)
        {
            printf("%c ",loc+'a');
        }
        abc[loc] = 0x7fffffff;
    }
    return 0;
}

//删除指定的子串,并统计删除的个数
int delsubstring(char src[],char de[])
{
    char *a;
    int n,num=0;
    n = strlen(de);
    while(1)
    {
        a = strstr(src,de);
        if(a == NULL)
            break;
        else
        {
            strcpy(a,a+n);
            num++;
        }
    }
    return num;
}

//判断b是否为a的子串,并统计b出现的次数
int isubstring(char a[],char b[]) 
{
    int n = 0,le = 0;
    char *buf=a;
    while(1)
    {
        buf = strstr(buf+le,b);
        le = strlen(b);
        if(buf==NULL)
            break;
        else
            n++;
    }
    return n;
}

//两个字符串求解相异部分
void deff_string(char str1[],char str2[])
{
    int len1,len2,i,j,ki,kj,del_num=0;
    len1 = strlen(str1);
    len2 = strlen(str2);
    for(i=0;i<len1;i++)
    {
        for(j=0;j<len2;j++)
        {
            if(str1[i]==str2[j])
            {
                ki=i;
                kj=j;
                del_num = 1;
                while(1)
                {
                    i++;
                    j++;
                    if(str1[i]==str2[j])
                        del_num = del_num + 1;
                    else
                        break;
                }
                if(del_num>1)
                {
                    strcpy(&str1[ki],&str1[i]);
                    strcpy(&str2[kj],&str2[j]);
                    i = 0;
                    j = 0;
                    len1 -= del_num;
                    len2 -= del_num;
                    str1[len1] = '\0';
                    str2[len2] = '\0';
                }
            }
        }
    }
    return ;
}
//求解两个字符串的最长公共子串,并返回最大子串的长度
int GetMaxCommStr(char a[],char b[],char *sub)
{
    int num=0,i,j,lena,lenb,len_com=0;
    char *buf;
    sub[0] = '\0';
    lena = strlen(a);
    lenb = strlen(b);
    for(i=0;i<lena;i++)
    {
        for(j=0;j<lenb;j++)
        {
            if(a[i]==b[j])
            {
                len_com = 1;
                buf = &b[j];
                while(1)
                {
                    i++;
                    j++;
                    if(a[i]==b[j])
                    {
                        len_com++;
                    }
                    else
                        break;
                }
                if(num<len_com)
                {
                    num = len_com;
                    strncpy(sub,buf,len_com);
                }
                j=0;
            }
        }
    }
    return num;
}
//指定字符串替换:将A中的B替换为C,返回替换的次数
int subreplace(char src[],char s_find[],char s_change[])
{
    char *a;
    char tem[100] = {'\0'};
    int n = strlen(s_find),num=0;
    while(1)
    {
        a = strstr(src,s_find);
        if(a==NULL)
            break;
        else
        {
            num++;
            strcpy(tem,s_change);
            strcat(tem,a+n);
            strcpy(a,tem);
        }
    }
    return num;
}
//把字符串嵌入母串的指定位置
void InsertStr(char *src,char *sub,int n)
{
    int len = strlen(src);
    char *temp;
    if(n<0||n>len)
    {
        return;
    }
    else
    {
        if(len - n == 1)
        {
            strcat(src,sub);
            return;
        }
        else
        {
            temp = &(src[n]);
            strcat(sub,temp);
            strcpy(src+n,sub);
            return;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值