C语言课程训练系统题-字符串cqupt

1.十进制转换十六进制

#include<stdio.h>
#include<string.h>
main()
{
	char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	int i=0,n;
	char b[10];
	printf("Input n:");
	scanf("%d",&n);
	while(n)
	{
		b[i++]=a[n%16];
		n/=16;
	}
	b[i]='\0';
	strrev(b);
	printf("%s",b);
}

2.比较两个字符串的大小

#include <stdio.h>
int main()
{
	int i,d=0;
	char a[10];
	char b[10];
	printf("Please enter two strings.\n");
	gets(a);
	gets(b);
	for(i=1;i<11;i++)
	{
		if(a[i]>b[i]) {d=1;printf("s1>s2");break;}
		if(a[i]<b[i]) {d=1;printf("s1<s2");break;}
	}
	if(d==0)printf("s1=s2");
	return 0;
}

3.输出最长的字符串

#include<stdio.h>
#include<string.h>
#define N 40
#define M 80
main()
{
	int i,count,max;
	char s[N][M];
	puts("Enter Text Lines,ende with empty line:");
	count=0;
	while(count<N)
	{
		gets(s[count]);
		if(!s[count][0])break;
		count++;
	}
	max=0;
	for(i=1;i<count;i++)
	{
		if(strlen(s[i])>strlen(s[max]))
			max=i;
	}
	puts(s[max]);
}

4.统计一个字符串在另外一个字符串出现的次数

#include<stdio.h>
#include<string.h>
main()
{
	char s1[100]="is",s[100]="This is a test!";
	int i=0,j=0;
	int count=0;
	while(s[i])
	{
		while(s1[j])
			if(s1[j++]!=s[i++])break;
			if(s1[j]=='\0')count++;
			j=0;
	}
	printf("\"%s\"在\"%s\"中出现了%d次。",s1,s,count);
}

5.有趣的“回文”检测

#include<stdio.h>
#include<string.h>
main()
{
	int i,n,d=1;
	char a[10];
	printf("Input string:");
	scanf("%s",a);
	n=strlen(a);
	for(i=0;i<(n/2);i++)
	{
		if(a[i]!=a[n-i-1]){d=0;break;}
	}
	if(d==1)printf("Yes!\n");
	else printf("No!\n");
}

6.输入一个字符串,将其逆序输出。

#include<stdio.h>
#include<string.h>
main()
{
	int i,n;
	char a[100],d;
	printf("Please Enter String1:\n");
	gets(a);
	n=strlen(a);
	for(i=0;i<(n/2);i++)
	{
		d=a[i];a[i]=a[n-i-1];a[n-i-1]=d;

	}
	 printf("Result is:\n%s\n",a);
}

7.输入一行字符,统计其中的英文字符、数字字符、空格字符,以及其他字符的个数

.#include <stdio.h> 
#include <string.h> 
  
#define ARR_SIZE 80 
  
main() 
{ 
    char str[ARR_SIZE]; 
    int  len, i; 
    int  letter=0,digit=0,space=0,other=0; 
  
    printf("请输入一个字符串:"); 
    gets(str); 
  
    len = strlen(str);     
  
    for (i=0; i<len; i++) 
    { 
        if (('a'<=str[i]&&str[i]<='z') ||('A'<=str[i]&&str[i]<='Z')) 
        { 
            letter++;    
        }                 
        else if('0'<=str[i]&&str[i]<='9')  
        { 
            digit++;       
        }             
        else if(str[i]==' ')    
        {                    
            space++;      
        }         
        else 
            other++;              
    } 
  
    printf("英文字符数:%d\n", letter); 
    printf("数字字符数:%d\n", digit); 
    printf("空格数:%d\n", space); 
    printf("其他字符数:%d\n", other); 
}

8.编程实现找出字符串中最大字符元素并输出该元素及其对应的ASCII值

#include<stdio.h>
#include<string.h>
main()
{
	int i,n,c;
	char a[20],d;
	printf("Input a string:\n");
	gets(a);
	n=strlen(a);
	d=a[0];
	for(i=1;i<n;i++)
	{
		if(a[i]>d){c=i;d=a[i];}
	}
	printf("The largest character of \"%s\" is \'%c\' ,The ASCII is %d.",a,a[c],a[c]);
}

9.字符串逆序

用字符数组作函数参数编程,利用一个数组实现字符串(允许输入带空格的字符串)的逆序存放。要求如下:
(1)在主函数中从键盘输入字符串,字符串的最大长度为80个字符。
调用Inverse()函数将字符串逆序存放,然后在主函数中输出逆序后的字符串。
(2)在子函数Inverse()中实现字符串的逆序存放。函数原型为:
void Inverse(char str[]);

#include<stdio.h>
#include<string.h>
void Inverse(char str[]);
main()
{
	char str[20];
	printf("Input a string:\n");
	gets(str);
	Inverse(str);
	printf("Inversed results:\n");
	puts(str);
}
void Inverse(char str[])
{
	int i,n,d;
	n=strlen(str);
	for(i=0;i<(n/2);i++)
	{
		d=str[i];str[i]=str[n-i-1];str[n-i-1]=d;

	}
}

10.编程实现从键盘输入5个国名(每个国名最长80个字符)

找出并输出按字典顺序排在最前面的国名

#include<stdio.h>
#include<string.h>
main()
{
	int i,n=0;
	char a[5][20],d[10];
	printf("Input five countries' names:\n");
	for(i=0;i<5;i++)
	{
		gets(a[i]);
	}
	strcpy(d,a[0]);
	for(i=1;i<5;i++)
	{
		if(strcmp(a[i],d)<0)
		{
			strcpy(d,a[i]);
			n=i;
		}
	}
	printf("The minimum is:%s\n",a[n]);
}

11.编写函数判断B是不是A的子串(假设A的长度大于B的长度,且两个字符串都不超过100个字符)。注意:串中任意个连续的字符组成的子序列称为该串的子串。

主函数中输入两个字符串,并调用上述函数判断,然后在主函数中输出”YES”,或者”NO”。

#include<stdio.h>
#include<string.h>
main()
{
	char s1[100],s[100];
	int i=0,j=0;
	int count=0;
	printf("Please input the first str:");
	scanf("%s",s);
	printf("Please input the second str:");
	scanf("%s",s1);
	while(s[i])
	{
		while(s1[j])
			if(s1[j++]!=s[i++])break;
			if(s1[j]=='\0')count++;
			j=0;
	}
	if(count==0)printf("NO\n");
	else printf("YES\n");
}

12.将字符串s1从第m个字符开始剩余的所有字符,送入字符数组s2中。

**输入格式要求:"%d" 提示信息:“input a string:\n” “input start point:\n”
**输出格式要求:puts(s2)
程序运行示例如下:
input a string:hello,world!
input start point:7
world!

#include<stdio.h>
#include<string.h>
main()
{
	char s1[100],s2[100]="0";
	int i,n,m,d=0;
	printf("input a string:\n");
	scanf("%s",s1);
	fflush(stdin);
	printf("input start point:\n");
	scanf("%d",&m);
	n=strlen(s1);
	for(i=m-1;i<n;i++)
	{
		s2[d]=s1[i];
		d++;
	}
	puts(s2);
}

13.下面的程序将数字进行加密,其中的每一个数字转化为另一个不同的数字

#include <stdio.h>

main()
{
    char s[] = "24635", c;
	int i;

    for (i = 0;i<5; i++)
    {
		c = s[i];
        switch(c)
        {
        case '2':putchar(c+4);break;
        case '3':putchar(c+4);break;
        case '4':putchar(c+4);break;
        case '5':putchar(c+3);break;
        default:putchar(c+2);
        }
        putchar('\n');
    }
}

14.编写一个程序,将一个字符串s2插入到字符串s1中,其起始插入位置为n。

**输入插入位置格式要求:"%d"

#include<stdio.h> 
#include<string.h> 
void main() 
{ 
	char s1[40],s2[20],ch3[20],i,j,temp,x;
	int n;
	printf("main string:"); 
	gets(s1); 
	printf("sub string:"); 
	gets(s2); 
	n=strlen(s1);
    printf("site of begining:(<=%d)",n); 
	scanf("%d",&i); 
	temp = i; 
	for(j=0;s1[temp] != '\0';j++,temp++) 
		ch3[j] = s1[temp]; 
	ch3[j]='\0'; 
    for(j=0;j<strlen(s1);j++,i++) 
		s1[i] = s2[j];  
	strcat(s1,ch3); 
	s1[i] = '\0'; 
	printf("After instert:%s\n",s1);
}

15.从键盘输入一个字符串(最长不超过80字符),然后从键盘输入一个字符,

删除字符串中与该字符相同的字符。其中,在字符串中删除与某字符相同的字符,要求用字符数组作函数参数编程实现。已知该函数的原型为:
void Squeeze(char s[], char c);
程序运行结果示例:
Input a string:
hello, my friend!↙
Input a character:
!↙
Results:hello, my friend

#include<stdio.h>
#include<string.h>
void Squeeze(char a[],char c);
main()
{
	char a[100],c;
	printf("Input a string:\n");
	gets(a);
	printf("Input a character:\n");
	c=getchar();
	Squeeze(a,c);
	printf("Results:%s\n",a);
}

void Squeeze(char a[],char c)
{
	int i,j,n,m,d;
	n=strlen(a);
	do{
		m=0;
		for(i=0;i<n;i++)
		{
			if(a[i]==c)
			{
				m=1;
				for(j=i;j<n-1;j++)a[j]=a[j+1];
				a[n-1]='\0';
			}
			
		}
		n=strlen(a);
	}while(m!=0);
}

16.//abc*d

#include<stdio.h>
#include<string.h>
void  Squeeze(char a[],char d[]);
main()
{
	char a[100],d[100];
	printf("请输入需要处理的字符串");
	gets(a);
	Squeeze(a,d);
	printf("处理后的字符串:");
	puts(d);
}

void  Squeeze(char a[],char d[])
{
	int i,j=0,n;
	n=strlen(a);
	for(i=0;i<n;i++)
	{
		if(j%2==0)d[i]=a[i];
		else
		{
			d[j]='*';j++;d[j]=a[i];
		}
		j++;
	}
	d[j]='\0';
}
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pitepa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值