栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言常用的字符串处理函数(代码实现)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言常用的字符串处理函数(代码实现)

常用函数:

strcat 函数——字符串连接函数

strcpy/strncpy 函数——字符串复制函数

strcmp 函数——字符串⽐较函数

strlen函数——测字符串长度的函数

strlwr函数——转换为⼩写的函数

字符串拼接(strcat)

指针法:(指针函数:返回一个地址)

#include
char *strc(char *str1,char *str2){
    char *p;
    for(p=str1;*p;p++);		 //定位到 s1字符串 中的'' 位置
    do{*p++=*str2++;}while(*str2);
    *p='';				//超级重要!!!
    return str1;
}
int main(){
    char s1[80]="abc",s2[]="vhfb",*pt;
    pt=strc(s1,s2);
    printf("%sn",pt);
    return 0;
}

指针法:

#include
void strc(char *p1,char *p2){
    char *p=p1;
    while (*p1)p1++;
    while (*p2)*p1++=*p2++;
    *p1='';
}
int main(){
    char s1[80]="abc",s2[]="vhfb";
    strc(s1,s2);
    puts(s1);
    return 0;
}

普通方法:

#include 
//将s2连接到s1后面, s2不改变
void strconcat(char *s1,char *s2){
    int i,j;
    for(i = 0;s1[i];i++);  //定位到 s1字符串 中的'' 位置
    for(j = 0;s2[j];i++,j++)
        s1[i] = s2[j];
    s1[i] = '';       //结束!!!超级重要!!!超级容易忘!!!
}
int main(){
    char a[100],b[100];
    gets(a);
    gets(b);
    strconcat(a,b);
    puts(a);
    return 0;
}

判断字符串是否相等(strcmp)

#include 
int flag=1;
void strp(char *s,char *t){
    int i=0;
    while (*(s+i)==*(t+i)&&*(s+i)!=0)i++;
    if(*(s+i)!=''||*(t+i)!='')flag=0;
}
int main(){
    char a[10],b[10];
    gets(a);
    gets(b);
    strp(a,b);
    if (flag)
        printf("两个字符串相等");
    else
        printf("两个字符串不相等");
    return 0;
}

字符串赋值(strcpy)

#include 
//s2字符串 复制 到s1中
void strcopy(char *s1,char *s2){
    while (*s2)*s1++=*s2++;
    *s1 = '';        //超级重要!!!
}
int main(){
    char s1[100],s2[100];
    gets(s2);
    strcopy(s1,s2);
    puts(s1);
}

字符串长度(strlen)

#include 
int strl(char *str){
    int len=0;
    while(*str++) len++;
    return len;
}
int main(){
    char str[100];
    gets(str);
    printf("%d",strl(str));
    return 0;
}

字符串大写转小写(strlwr)

#include 
// 大写转小写
void strtol(char *str)
{
    while (*str){
    *str += 32;
    str++;
    }
}
int main()
{
    char str[100];
    gets(str);
    strtol(str);
    puts(str);
    return 0;
}

字符串小写转大写(strlwr)

#include 
// 小写转大写
void strtou(char *str)
{
    while (*str){
        *str -= 32;
        str++;
    }
}
int main()
{
    char str[100];
    gets(str);
    strtou(str);
    puts(str);
    return 0;
}

拓展
例1:将输入的字符串中每个单词首字母转为大写

#include 
#define TRUE 1
#define FALSE 0
int change(char *c,int status)
{
    if(*c==' ')return TRUE;
    else{
        if (status&& *c<='z'&&*c>='a')*c -=32;
        return FALSE;
    }
}
int main()
{
    int flag=TRUE;
    char ch;
    do{
        ch=getchar();
        flag=change(&ch,flag);
        putchar(ch);
    }while (ch!='.');
    printf("n");
    return 0;
}

输入:this is a text.
输出:This Is A Text.

例2:找出一个英语句子中最长单词有几个字母

#include
int main(){
    char *p,a[]={"I am happy ."};
    int max=0,len=0;
    p=a;
    while (*p!='.')
    {
        while (((*p<='Z')&&(*p>='A')||(*p<='z')&&(*p>='a')))
        {
            len++;
            p++;
        }
        if(len>max)max=len;
        len=0;p++;
    }
    printf("max=%d",max);
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863998.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号