C语言程序设计题目36至40

C语言程序设计题目36

不使用库函数,计算字符串长度

#include <stdio.h>
#include <stdlib.h>


int main()
{
	char s[1000], i;
	printf("输入字符串:");
	scanf("%s", s);
	for (i = 0; s[i] != '\0'; i++)
	{
		
	}
	printf("字符串长度:%d\n", i);
	system("pause");
	return 0;
}

C语言程序设计题目37

查找字符在字符串中出现的次数

#include <stdio.h>
#include <stdlib.h>


int main()
{
	char str[1000], ch;
	int i, frequency = 0;
	printf("输入字符串:");
	fgets(str, (sizeof(str) / sizeof(str[0])), stdin);
	printf("输入要查找的字符:");
	scanf("%c", &ch);
	for (i = 0; str[i] != '\0'; i++)
	{
		if (ch == str[i])
		{
			frequency++;
		}
	}
	printf("字符%c在字符串出现的次数为%d\n", ch, frequency);
	system("pause");
	return 0;
}

C语言程序设计题目38

不使用库函数,字符串复制

#include <stdio.h>
#include <stdlib.h>


int main()
{
	char s1[100], s2[100], i;
	printf("字符串s1:");
	scanf("%s", s1);
	for (i = 0; s1[i] != '\0'; i++)
	{
		s2[i] = s1[i];
	}
	s2[i] = '\0';
	printf("字符串s2:%s\n", s2);
	system("pause");
	return 0;
}

C语言程序设计题目39

按字典顺序排序字符

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int  i, j;
	char str[10][50], temp[50];
	printf("输入10个单词:\n");
	for (i = 0; i < 10; i++)
	{
		scanf("%s", str[i]);
	}
	for (i = 0; i < 9; i++)
	{
		for (j = i + 1; j < 10; j++)
		{
			if (strcmp(str[i], str[j]) > 0)
			{
				strcpy(temp, str[i]);
				strcpy(str[i], str[j]);
				strcpy(str[j], temp);
			}
		}
	}
	printf("\n排序后:\n");
	for (i = 0; i < 10; i++)
	{
		puts(str[i]);
	}
	system("pause");
	return 0;
}

C语言程序设计题目40

结构体的简单使用

#include <stdio.h>
#include <stdlib.h>


struct student
{
	char name[50];
	int roll;
	float marks;
};

int main()
{
	student s;
	printf("输入信息:\n");
	printf("名字:");
	scanf("%s", &s.name);
	printf("编号:");
	scanf("%d", &s.roll);
	printf("成绩:");
	scanf("%f", &s.marks);
	printf("显示信息:\n");
	printf("名字:");
	puts(s.name);
	printf("编号:%d\n", s.roll);
	printf("成绩:%.lf\n", s.marks);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值