⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ruanjian.cpp

📁 字符串处理的大部分函数函数 能够实现很多字符串的复杂处理功能
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define  N  50
#define  M  100
void main()
{  
	int  x;
	char c;
	char s;
	char str[M];
    int letters=0;
	int space=0;
	int digit=0;
    int others=0;
	int n,m,temp,i,j,k,num[N];
	int l=0;
	char cc,n1;
    int w=0;
	while(1)
	{
		printf("\n\n 1,对整数数组排序 \n");
		printf(" 2,计算文本中非空格字符的个数\n");
		printf(" 3,用单个空格代替文本行中的所有空格 \n");
		printf(" 4,连接字符串及求字符串长度和取子字符串\n");
		printf(" 5,退出\n");
		scanf("%c",&s);
        getchar();
		if(s=='1')
		{
			printf("请输入要排序的数组:\n");
			scanf("%d",&num[w]);
			while(1)
			{
				if(((cc=getchar())=='  '))
				{ 
					w++;
					scanf("%d",&num[w]);
				}
				else
				{  
					w++;
					break;   
				}
			}

			for(i=0;i<w;i++)
			{
				for(j=0;j<w-(i+1);j++)
				{
					if(num[j+1]<num[j])
					{
						temp=num[j+1];
						num[j+1]=num[j];
						num[j]=temp;
					}
				}
			}
			for(m=0;m<w;m++)
				printf(" %d ",num[m]);
			printf("\n");
	//	exit(0);
			w=0;	
		}
		
	if(s=='2')
		{
			printf("请输入要计算的文本:\n");
		//	getchar();
// loop:   
	    	while((c=getchar())!='\n')
			{
		
               
			if((c>='a'&& c<='z')||(c>='A'&&c<='Z'))
				letters++;
			else if(c>='0'&&c<='9')
				digit++;
			else if(c==' ')
				space=0;
			else 
				others++;
			}
			x=letters+digit+others;
			printf("所求文本非空格字符的长度为:%d \n",x);
	        letters=0;
	        space=0;
	        digit=0;
            others=0;
		//	printf(	"请输入要计算的文本:");
			//goto loop;
			//exit(0);
	}
	if(s=='3')
		{
			    char  str0[M];
			    int z ;
				int j=0;
				int   k,space;
				int lenth;
				printf("请输入带有空格的字符串:\n");
				gets(str0);
				
				lenth = strlen(str0);
		
				while(str0[j]!='\0')
				{
					if(str0[j]==' ')
					{
						space=0;
						//count how many space need to delete
						for(k=j+1;k<lenth; k++ )
						{
							if(str0[k]==' ')
								space++;
							else
								break;
						}
						
						if(space>0)
						{
							//move the non-space character to the space
							for(k=j+space+1; k<lenth; k++)
							{
								str0[k-space] = str0[k];
							}

							//replace the txt in the last with '\0'
							for(k=lenth-1;  k>lenth-space; k--)
								str0[k] ='\0';
						}					

					}
					j++;
				}
				printf("%s \n", str0);
			//	exit(0);
		}

				
		if(s=='4')
		{
			char str1[M];
			char str2[M];
			printf("请输入字符串 1:\n");
			gets(str1);
			printf("%d\n",strlen(str1));
            printf("请输入字符串 2:\n");
            gets(str2);
			printf("%d\n",strlen(str2));
		    printf("%s\n",strcat(str1,str2));
			printf("%d\n",strlen(str1));
            printf("请输入你要选择子字符串的范围n<n1-n>=");
			scanf("%d",&n1);
			scanf("%d",&n);
	        l=strlen(str1);
	      //  strcpy(str[M],str1[M]);
		//	printf("%d\n",l);
			if(n>l)
				printf("你选择的字符超出了所输入的总长度!!!!!!\n");
			else
			{
				for(k=n1;k<=n;k++)
				{
				printf("%c",str1[k]);

				}
			
		}
			 printf("\n");
		}
		if(s=='5')
	{
		exit(0);

	}
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -