字符串处理

本文介绍了一组自定义的字符串处理函数,包括获取字符串长度、截取子串、字符串查找等实用功能,并提供了完整的C语言代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自编的几个字符串处理小函数

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
char a[100];
void menu()
{
	printf("欢迎使用字符串系统\n");
	printf("1:获取已知字符串的实际长度\n");
	printf("2:获取已输入字符串左端或右端长度为N的字符串\n");
	printf("3:根据给定的起始位置和长度获取部分字符串\n");
	printf("4:判断给定字符在已知字符串中\n");
	printf("5:将字符串反转\n");
	printf("6:将字符串中的所有字符变换为大写字符\n");
	printf("0:退出\n");
	printf("\n请输入你的选择:\n");
}
void huozhichangdu()
{
	printf("请输入字符串\n");
    scanf("%s",&a);
	printf("%d\n",strlen(a));
    system("pause");
}
void zuobian()
{
    int n,j,i;
	char b[100]="0";
	printf("请输入字符串\n");
    scanf("%s",&a);
	printf("请输入取数范围\n");
    scanf("%d",&n);
	i=strlen(a);
	if(n>i)
	{
		printf("%s",a);
		system("pause");
	}
	else
	{
		for(j=0;j<=n-1;j++)
			b[j]=a[j];
		printf("%s",b);
        system("pause");
	}
}
void youbian()
{
    int n,j,i,k;
	char b[100]="0";
	printf("请输入字符串\n");
    scanf("%s",&a);
	printf("请输入取数范围\n");
    scanf("%d",&n);
	i=strlen(a);
	if(n>i)
	{
		printf("%s",a);
		system("pause");
	}
	else
	{
		for(j=i-1,k=0;j<=(i-n+1),k<+n;j--,k++)
			b[k]=a[j];
		printf("%s",b);
		system("pause");
	}
}
void huoquyizhidezifuchuan()
{
	int i;
	printf("1:从左边开始");
	printf("2:从右边开始");
	scanf("%d",&i);
	switch(i)
	{
	case 1:
		zuobian();
		break;
	case 2:
		youbian();
		break;
	}
}
void huoquzhongjian()
{
	int i,j,k,m;
	char b[100]="0";
	printf("请输入字符串\n");
    scanf("%s",&a);
	printf("请输入起点位置");
    scanf("%d",&i);
	printf("请输入终点位置");
    scanf("%d",&j);
	if(j>=strlen(a))
            printf("%s",a);
	else
	{
		for(k=i,m=0;k<j+1;k++,m++)
	        	b[m]=a[k];
	   printf("%s",b);
	   system("pause");
	 }
}
void panduanzifu()
{
	char b;
	int i;
       getchar();
	printf("请输入字符\n");
	scanf("%c",&b);
	printf("请输入字符串\n");
    scanf("%s",&a);
	for (i=0;i<=strlen(a);i++)
	{
		if(a[i]==b)
	         break;
	}
	if(i<=strlen(a))
	{
		printf("%d\n",i);
	system("pause");
	}
    else
	{
		printf("不存在");
	    system("pause");
	}
}
void fanzhuan()
{
	printf("请输入字符串\n");
    scanf("%s",&a);
	char b[100]="0";
	int i,k;
	for (i=strlen(a),k=0;i>=0;i--,k++)
		b[k]=a[i-1];
     printf("%s",b);
     system("pause");
}
void bianhuan()
{
	int i;
	printf("请输入字符串\n");
    scanf("%s",&a);
	for(i=0;i<=strlen(a);i++)
	{
		if(a[i]<='z'&&a[i]>='a')
			a[i]=a[i]-32;
	}
	printf("%s",a);
     system("pause");

}
int main()
{
	int key;
	while(1)
	{
        system("cls");
		menu();
		scanf("%d",&key);
		switch(key)
		{
		case 1:
			huozhichangdu();
			break;
		case 2:
			huoquyizhidezifuchuan();
			break;
		case 3:
			huoquzhongjian();
			break;
		case 4:
			panduanzifu();
			break;
		case 5:
			fanzhuan();
			break;
		case 6:
			bianhuan();
			break;
		case 0:
		    printf("感谢您的支持,再见!");
			return 0;
		default:
			printf("你的输入有误,请重新输入:\n");
			system("pause");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值