1. 对输入的8个字符串(每个字符串长度不超过20)按照字典顺序进行排序并输出。不限定排序方法,不能使用goto语句。 输入输出格式: 输入:%s 输出:%s\n 输入:多个字符串用空格分隔 输出:排序好的字符串,每行一个字符串,末尾有空行。 输入输出样例: 输入: diankuang liuxu sui fengwu qingbo taohua zhu shuiliu 输出: diankuang fengwu liuxu qingbo shuiliu sui taohua zhu
#include<stdio.h>
#include<string.h>
#define MAXN 8 // 最大个数
#define MAXL 20 // 最大长度
int main (void)
{
char str[MAXN][MAXL];
int i, j;
for (i = 0; i < MAXN; i ++)
{
scanf("%s", str[i]);
}
for (i = 0; i < MAXN - 1; i ++)
{
for (j = i + 1; j < MAXN; j ++)
{
// 比对首字母, 冒泡排序交换字符串
if (strcmp(str[i], str[j]) > 0)
{
// 临时数组 temp 保存待交换数组
char temp[MAXL];
strcpy(temp, str[j]);
strcpy(str[j], str[i]);
strcpy(str[i], temp);
}
}
}
for (i = 0; i < MAXN; i ++)
{
printf("%s\n", str[i]);
}
return 0;
}
2. 写一个函数,将一个字符串中的元音字母复制到另一个字符串,然后输出。 程序的运行示例如下: 提示信息:"\n输入字符串:" 字符串中的元音字母是eoo ***输入数据格式***:使用gets() ***输出数据格式***:"\n字符串中的元音字母是%s" 程序运行示例: 输入字符串:Hello,world!↙ Hello,world! 字符串中的元音字母是eoo
#include <stdio.h>
#include <string.h>
int main (void)
{
char str[80]; // 读入用户输入字符串
char Str[80]; // 保存元音字母
printf ("\n输入字符串:");
gets (str);
int i = 0;
int index = 0;
while (str[i] != '\0')
{
if (str[i] == 'a' || str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u')
{
Str[index ++] = str[i];
}
i ++;
}
Str[index] = '\0';
puts (str);
printf ("字符串中的元音字母是%s", Str);
return 0;
}
3.
编写一个程序,将用户输入的由数字字符和非数字字符组成的字符串中的数字提取出来(例如:输入asd123,34fgh_566kkk789,则产生的数字分别是123、34、789)。
**输入格式要求:提示信息:"Please enter a string:"
**输出格式要求:"the result of output:\n" "%10d\n"
程序运行示例如下:
Please enter a string:
abc123def456ghi111bbbccc99go100
the result of output:
123
456
111
99
100
#include <stdio.h>
#include <stdbool.h>
void GetDigits (char *str);
int main (void)
{
char str[80];
printf ("Please enter a string:");
gets (str);
printf ("the result of output:\n");
GetDigits (str);
return 0;
}
void
GetDigits (char *str)
{
int cnt = 0;
int i = 0;
while (str[i] != '\0')
{
int num = 0;
if (isdigit (str[i]) == true)
{
while (isdigit (str[i]) == true)
{
int a = str[i] - '0';
num = num * 10 + a;
i ++;
}
printf ("%10d\n", num);
}
i ++;
}
}
4.
请按给定的函数原型编程实现将字符数组中的字符串的第m个字符开始的n个字符逆序存放。要求在主函数读入字符串,且逆序存放后的字符串也在主函数打印。函数原型: void inverse(char str[],int m,int n); 输入要求: 输入的一行字符串,应包含字母,数字以及空格字符 友情提示: 在执行输入字符串的函数之前,请用getchar();把输入缓冲区中的换行符读出!!! ****输入提示信息和格式要求为: "input m,n:" "%d,%d" "input the string:" ****输出格式为:"the inverse string:%s"
#include<stdio.h>
void inverse (char str[], int m, int n);
int main (void)
{
char str[50];
int m, n;
printf("input m,n:");
scanf("%d,%d", &m, &n);
getchar();
printf("input the string:");
gets(str);
inverse(str, m, n);
printf("the inverse string:%s", str);
return 0;
}
void
inverse (char str[], int m, int n)
{
int i, j;
for (i = m - 1, j = n + m - 2; i < n / 2 + m - 1; i ++, j --)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
5.
对输入的字符串进行长度验证,保证输入的字符串的长度在指定的范围内,如果不在指定的范围内,则一直提示用户输入,直到输入合法长度的字符串为止。 程序的示例运行如下: 请输入一个字符串(长度为[3..5]个字符):a 请输入一个字符串(长度为[3..5]个字符):ab 请输入一个字符串(长度为[3..5]个字符):abcdef 请输入一个字符串(长度为[3..5]个字符):abc 你输入的字符串为:abc
#include <stdio.h>
int CheckSize (char str[]);
int main (void)
{
int str[50];
int flag = 0; // 判断是否满足条件
for (; flag != 1; )
{
printf ("请输入一个字符串(长度为[3..5]个字符):");
gets (str);
flag = CheckSize (str);
}
printf ("你输入的字符串为:%s", str);
return 0;
}
int CheckSize (char str[])
{
int i = 0;
while (str[i] != '\0')
{
i ++;
}
if (i >= 3 && i <= 5)
{
return 1;
}
return 0;
}
int CheckSize (char str[])
{
int i = 0;
while (str[i] != '\0')
{
i ++;
}
if (i >= 3 && i <= 5)
{
return 1;
}
return 0;
}
6.
自己定义一个函数,用字符数组作为函数参数的类型, 以实现和函数strlen相同的功能,即在主函数中任意输入 一个字符串(长度不超过80个字符),调用该函数计算 输入字符串的实际长度,然后打印计算结果。 要求按如下函数原型编程实现计算字符串长度的功能。 int Mystrlen(char str[]); 要求必须按照题目要求和用函数编程,否则不给分。 **要求输入提示信息格式为:"Enter a string:" **输出格式为:"The length of the string is:%d\n"
#include <stdio.h>
int Mystrlen (char str[]);
int main (void)
{
char str[80];
printf ("Enter a string:");
gets (str);
printf ("The length of the string is:%d\n", Mystrlen (str));
return 0;
}
int
Mystrlen (char str[])
{
int i = 0;
while (str[i] != '\0')
{
i ++;
}
return i;
}
7.
写一个函数,输入一行字符,将此字符串中最长的单词输出。 程序的运行示例如下: 输入一行文本:I am a student. 最长的单词是:student
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
void LongWord (char str[]);
int main (void)
{
char str[80];
printf ("输入一行文本:");
gets (str);
printf ("\n\n");
LongWord (str);
return 0;
}
void
LongWord (char str[])
{
char word[30]; // word 存储最长的单词
char temp[30]; // temp 存储str中的单词
word[0] = '\0';
int len = strlen(str);
int i, j = 0;
for (i = 0; i < len; i ++)
{
j = 0;
while (isalpha (str[i]))
{
temp[j ++] = str[i ++];
}
temp[j] = '\0';
if (strlen(word) < strlen(temp))
{
strcpy(word, temp);
}
}
printf ("最长的单词是:%s", word);
}
8.
实现验证输入的必须全部是数字0~9构成的字符串,利用函数checkNumber验证输入的字符串中的每个字符是否全为数字0~9之一,如果是则返回1,否则返回0。然后利用函数getNumberString容错地读取一个全由数字0~9组成的数字字符串。要求编写完整的程序测试运行你所写的函数。 输入语句为:"请输入一个全为数字的字符串:\n“ 输出语句为:"你输入的正确的数字串为:%s\n" 程序的运行示例如下: 请输入一个全为数字的字符串: abc 请输入一个全为数字的字符串:a123 请输入一个全为数字的字符串:1a23 请输入一个全为数字的字符串:123 你输入的正确的数字串为:123
#include <stdlib.h>
#include <stdbool.h>
bool checkNumber (char *str);
void getNumberString (char *str);
int main (void)
{
char str[80];
while (1)
{
printf ("请输入一个全为数字的字符串:\n");
gets (str);
bool bFlag = checkNumber (str);
if (bFlag == true)
{
printf ("你输入的正确的数字串为:%s\n", str);
break;
}
}
return 0;
}
bool
checkNumber (char *str)
{
char *p = NULL;
p = str;
while (*p != '\0')
{
if (*p <= '0' || *p >= '9')
{
return false;
}
p ++;
}
return true;
}