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;
}