第11章 字符串和字符串函数—编程练习(未完结……)

1、设计并测试一个函数,从输入中获取n个字符(包括空白、制表符、换行符),把结果存储在一个函数里,它的地址被传递作为一个参数。

答案:传递的参数是:地址,字符长度
#include <stdio.h>
#include<string.h>
#define LEN 10
char *s_gets(char *st, int n);
int main(void)
{
	char str[LEN];
	char * val;
	puts("Enter string:");
	val = s_gets(str, LEN - 1);
	puts("Output string:");
	if (val)
	{
		puts(str);
		printf("Successful!\n");
	}
	else
		printf("Faile!\n");
	return 0;
}
char *s_gets(char *st, int n)
{
	int ret_val;
	int i = 0;
	//char ch;

	//停止读入的条件:1、达到长度限制2、第一个换行符
	//ret_val = fgets(st, n,stdin);

	/*getchar 等函数的返回值类型都是 int 型,
	当这些函数读取出错或者读完文件后,会返回 EOF.*/
	for (i = 0; i < n; i++)
	{
		ret_val = getchar();
		if (ret_val)
			st[i] = ret_val;
		else//ret_val==NULL
			break;
	}
	if (ret_val)
		//return st[i];
	{
		st[i] = '\0';
		return st;
	}
	else
		return NULL;
}
自己编写:传递参数为:地址
方法一:
#include<stdio.h>
#define LEN 100
char *getnchar(char* str);
int main(void)
{
	char str[LEN];
	puts("Enter characters:");
	getnchar(str);
	printf("%s\n", str);
	return 0;
}
char *getnchar(char* str)
{
	int index = 0;
	while ((str[index] = getchar()) != EOF)
	{
		index++;
	}
	if (str[index] == EOF)
		str[index] = '\0';

	return str;
}


方法二:
#include<stdio.h>
#define SIZE 10
char * getstr(char *str, int n);
int main(void)
{
 char array[SIZE];
 char *val;
 printf("Enter string:\n");
 val = getstr(array, SIZE - 1);
 printf("Output string\n");
 if (val)
  puts(array);
 else
  puts("Failed!");

 printf("Well done!\n");

 return 0;
}
char * getstr(char *str, int n)
{
 int num = 0;;
 char ch;
 while (ch = getchar())
 {
  if (ch != EOF&&num < n)
  {
   str[num] = ch;
   num++;
  }
  else
   break;
 }
 str[num] = '\0';
 if (num == 0)
  return NULL;
 else
  return str;
 /*if (ch == EOF)
  return NULL;
 else
 {
  str[num] = '\0';
  return str;
 }*/
}

2、修改编程练习1的函数,在n个字符后停止,或在读到第一个空白、制表符或换行符时停止,哪个选遇到哪个停止。不能只使用scanf()。

#include <stdio.h>
#include<string.h>
#include<ctype.h>
#define LEN 10
char *s_gets(char *st, int n);
int main(void)
{
	char str[LEN];
	char * val;
	puts("Enter string:");
	val = s_gets(str, LEN - 1);
	puts("Output string:");
	if (val)
	{
		puts(str);
		printf("Successful!\n");
	}
	else
		printf("Faile!\n");
	return 0;
}
char *s_gets(char *st, int n)
{
	int ret_val;
	int i = 0;
	
	for (i = 0; i < n; i++)
	{
		ret_val = getchar();
		if (!isspace(ret_val))
			st[i] = ret_val;
		else//ret_val==NULL
			break;
	}
	if (ret_val)
	{
		st[i] = '\0';
		return st;
	}
	else
		return NULL;
}
【注】与想要的效果的不一样
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#define LEN 10
char *s_gets(char *st, int n);
int main(void)
{
	char str[LEN];
	char * val;
	puts("Enter string:");
	val = s_gets(str, LEN - 1);
	puts("Output string:");
	if (val)
	{
		puts(str);
		printf("Successful!\n");
	}
	else
		printf("Faile!\n");
	return 0;
}
char *s_gets(char *st, int n)
{
	int ret_val;
	int i = 0;

	for (i = 0; i < n; i++)
	{
		ret_val = getchar();

		if (!isspace(ret_val))
			st[i] = ret_val;
		else//ret_val==NULL
			st[i] = '\0';
	}
		return st;
}
【注】还是不够好………………

3、设计并测试一个函数,从一行输入中把一个单词读入一个数组中,并丢弃输入行中其他的字符。该函数应该跳过第一个非空白字符前面的所有空白。将一个单词定义为没有空白、制表符或换行符的字符序列。

11.3
#include<stdio.h>
#include<ctype.h>
#define LEN 100

char *getschar(char *string);
int main(void)
{
	char arr[LEN];
	char *point;
	
	if(point)
		puts(string);
	else 
		puts("Fail!");
		
	return 0;
}
char *getschar(char *string)
{
	char *re_val;
	char ch;
	
	re_val=string;
	while((ch=getchar())=EOF&&isspace(ch))//跳过第一个字母前的空格
		continue;
	if(ch==EOF)
		return ret_val=NULL;
	while(ch!=EOF&&!isspace(ch))
	{
		*string=ch;//获得第一个字母
		string++;
		ch=getchar();
	}
	*string='\0';
	if(ch==EOF)
		return ret_val;
	else
	{
		while(getchar()!=EOF)
			continue;
		return ret_val;
	}
}
		
	

4、设计并测试一个函数,它类似编程练习3的描述,只不过它接受第2个参数指明可读取的最大字符数。

**//最大的字符数为9**
#include<stdio.h>
#include<ctype.h>
#define LEN 10

char *getschar(char *str, int n);
int main(void)
{
	char str[LEN];
	char *val;

	puts("Please a string:");
	val = getschar(str, LEN-1);

	if (val)
		puts(str);
	else
		puts("Fail!");
	puts("Well done!");
	
	return 0;
}
char *getschar(char *str, int n)
{
	int index=0;
	char *ret_val;
	char ch;

	ret_val = str;

	while ((ch = getchar()) != EOF&&isspace(ch))
		continue;
	if (ch == EOF)
		return ret_val = NULL;
	else
	{
		str[index] = ch;
		index++;
	}
	while ((ch = getchar()) != EOF && !isspace(ch) && index < n)
	{
		str[index] = ch;
		index++;
	}
	str[index] = '\0';                                  
	if (ch == EOF)
		return ret_val;
	else
		while (getchar() !=EOF )
			continue;
	return ret_val;
}

5、测试删除多余字符未成功
设计并测试一个函数,搜索第1个函数形参指定的字符串,在其中查找第
2个函数形参指定的字符首次出现的位置。如果成功,该函数返指向该字
符的指针,如果在字符串中未找到指定字符,则返回空指针(该函数的
功能与 strchr()函数相同)。在一个完整的程序中测试该函数,使用一个
循环给函数提供输入值。

#include<stdio.h>
#define LEN 100

char *getstr(char *str, int n);//输入字符串
char *find(char* str, char ch);//遍历字符串,查找字符

int main(void)
{
	char str[LEN];
	char ch;
	char *val;

	puts("Please enter a string:");
	getstr(str, LEN);

	puts("Please enter the character you want to find");
	while ((scanf_s("%c", &ch, 1)) != 1)
		puts("Please re-enter the characters");
	getchar();

	val = find(str, ch);
	if (val)
		printf("The address of character %c is %p\n", ch, val);
	else
		printf("Fail!\n");

	printf("Done!\n");

	return 0;

}

//输入字符串
char *getstr(char *str, int n)
{
	char ch;
	int index = 0;
	//跳过开始输入的无效的字符
	while ((ch = getchar()) == EOF)
		continue;

	str[index] = ch;//获得第一个有效字符
	index++;
	//获取其他字符
	while ((ch = getchar()) != EOF&&index<n)
	{
		str[index] = ch;
		index++;
	}
	str[index] = '\0';

	//输出字符串的地址,判断find()的返回值是否正确
	printf("The address of the string is %p\n", str);
	return str;
}

//遍历字符串,查找字符
char* find(char* str, char ch)
{
	char *ret;

	ret = str;
	while (*++str != '\0')
	{
		if (*str != ch)
			continue;
		else
			return ret = str;
	}
	//if (*str == '\0')
	return ret = NULL;
}

6.编写一个名为is_within()的函数,接受一个字符和一个指向字符串的指
针作为两个函数形参。如果指定字符在字符串中,该函数返回一个非零
值(即为真)。否则,返回0(即为假)。在一个完整的程序中测试该函
数,使用一个循环给函数提供输入值。

#include<stdio.h>
#include<string.h>
#define LEN 100

int is_within(char *string, char ch);
char* s_getchar(char *string, int n);
int main(void)
{
	int index;
	char string[LEN];
	char ch;

	printf("Please enter a string:\n");
	while (s_getchar(string,LEN))
	{
		printf("Please enter the characters:\n");
		while (!scanf_s("%c", &ch,1))
			printf("Please re-enter the characters:\n");
		getchar();

		index = is_within(string, ch);
		if (index)
			printf("%c is in the target string\n", ch);
		else
			printf("The specified character was not found.\n");
		printf("Please continue to enter a set of strings:\n");
	}

	printf("Well done!\n");

	return 0;
}
char* s_getchar(char *string, int n)
{
	char *ret_val;
	int index=0;

	ret_val = fgets(string, n, stdin);

	if(ret_val)
	{ 
		while (string[index] != '\n'&&string[index] != '\0')
			index++;
		if (string[index] == '\n')
			string[index] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return ret_val;
}
int is_within(char *string, char ch)
{
	int judge = 0;
	char *ret_val;

	ret_val = strchr(string, ch);

	if (ret_val)
		return judge = 1;
	else
		return judge = 0;
}

7.strncpy(s1, s2, n)函数把s2中的n个字符拷贝至s1中,截断s2,或者有
必要的话在末尾添加空字符。如果s2的长度是n或多于n,目标字符串不
能以空字符结尾。该函数返回s1。自己编写一个这样的函数,
名为mystrncpy()。在一个完整的程序中测试该函数,使用一个循环给函
数提供输入值。

#include<stdio.h>
#include<string.h>
#define LEN 10
//#define WID 100

char *s_gets(char *temp, int n);
char *mystrncpy(char *string, char *temp, int n);
int main(void)
{
	char string[LEN];
	char temp[LEN];
	int index = 0;
	char *ps;

	printf("Please enter a string:\n");
	while (s_gets(temp, LEN))
	{
		ps=mystrncpy(string, temp, LEN-1);
		puts(ps);
		printf("Please enter a string:\n");
	}

	printf("Well done!\n");
	return 0;
}
char *s_gets(char *temp, int n)
{
	char *ret_val;
	int index = 0;

	ret_val = fgets(temp, n, stdin);
	if (ret_val)
	{
		while (temp[index] != '\0'&&temp[index] != '\n')
			index++;
		if (temp[index] == '\n')
			temp[index] = '\0';
		else
		{
			while (getchar() != '\n')
				continue;
		}
	}
	return ret_val;
}
char *mystrncpy(char *string, char *temp, int n)
{
	char *ret;
	int index = 0;

	ret = string;
	while (index <= n&&*temp != '\0')
	{
		*string = *temp;
		string++;
		temp++;	
		index++;
	}
	*string = '\0';

	return ret;
}

8.编写一个名为string_in()的函数,接受两个指向字符串的指针作为参
数。如果第2个字符串中包含第1个字符串,该函数将返回第1个字符串
开始的地址。例如,string_in(“hats”, “at”)将返回hats中a的地址。否则,
该函数返回空指针。在一个完整的程序中测试该函数,使用一个循环给
函数提供输入值。

#include<stdio.h>
#include<string.h>
#define LEN 100


char *s_gets(char *temp, int n);
char *string_in(char *target, char *source);
int main(void)
{
	char source[LEN];
	char target[LEN];
	char *address;

	printf("Please enter the target string:\n");
	while (s_gets(target, LEN))
	{
		printf("Please enter the source string:\n");
		s_gets(source, LEN);
		address = string_in(target, source);
		if (address)
			printf("Address is %p\n", address);
		else
			printf("Fail!\n");
		printf("Please enter the target string:\n");
	}

	printf("Well done!\n");
	return 0;
}
char *s_gets(char *temp, int n)
{
	char *ret_val;
	int index = 0;

	ret_val = fgets(temp, n, stdin);
	if (ret_val)
	{
		while (temp[index] != '\0'&&temp[index] != '\n')
			index++;
		if (temp[index] == '\n')
			temp[index] = '\0';
		else
		{
			while (getchar() != '\n')
				continue;
		}
	}
	return ret_val;
}
char *string_in(char *target, char *source)
{
	char *ret_val;

	ret_val = target;

	if (strlen(target) < strlen(source))
		return NULL;

	while (*target != *source&&*target != '\0')
	{
		target++;
		continue;
	}
	if (*target == '\0')
		return ret_val = NULL;
	else
	{
		ret_val = target;
		target++;
		source++;
	}

	while (*target == *source&&*target != '\0')
	{
		target++;
		source++;
	}
	if (*source == '\0')
		return ret_val;
	else
		return NULL;
}

9.编写一个函数,把字符串中的内容用其反序字符串代替。在一个完整
的程序中测试该函数,使用一个循环给函数提供输入值。

#include<stdio.h>
#include<string.h>
#define LEN 40

char *s_gets(char *temp, int n);

int main(void)
{
	char string[LEN];
	int len;
	int index;

	printf("Please enter a string:\n");
	while (s_gets(string, LEN))
	{
		len = strlen(string);
		//index = len-1,数组下表是从0开始,下表比数量小1
		for (index = len-1; index >= 0; index--)
			printf("%c", string[index]);
		printf("\n");
		printf("Please enter a string:\n");
	}
	printf("Well done!\n");
	return 0;
}
char *s_gets(char *temp, int n)
{
	char *ret_val;
	int index = 0;

	ret_val = fgets(temp, n, stdin);
	if (ret_val)
	{
		while (temp[index] != '\0'&&temp[index] != '\n')
			index++;
		if (temp[index] == '\n')
			temp[index] = '\0';
		else
		{
			while (getchar() != '\n')
				continue;
		}
	}
	return ret_val;
}

10.编写一个函数接受一个字符串作为参数,并删除字符串中的空格。
在一个程序中测试该函数,使用循环读取输入行,直到用户输入一行空行。
该程序应该应用该函数只每个输入的字符串,并显示处理后的字符串。

在这里插入代码片

11.编写一个函数,读入10个字符串或者读到EOF时停止。该程序为用
户提供一个有5个选项的菜单:打印源字符串列表、以ASCII中的顺序打印字
符串、按长度递增顺序打印字符串、按字符串中第1个单词的长度打印字符
串、退出。菜单可以循环显示,除非用户选择退出选项。当然,该程序要能
真正完成菜单中各选项的功能。

在这里插入代码片

12.编写一个程序,读取输入,直至读到 EOF,报告读入的单词数、大
写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h头文件中的
函数。

在这里插入代码片

13.编写一个程序,反序显示命令行参数的单词。例如,命令行参数是
see you later,该程序应打印later you see。

在这里插入代码片

14.编写一个通过命令行运行的程序计算幂。第1个命令行参数是double
类型的数,作为幂的底数,第2个参数是整数,作为幂的指数。

在这里插入代码片

15.使用字符分类函数实现atoi()函数。如果输入的字符串不是纯数字,
该函数返回0。

在这里插入代码片

16.编写一个程序读取输入,直至读到文件结尾,然后把字符串打印出
来。该程序识别和实现下面的命令行参数:
-p     按原样打印
-u     把输入全部转换成大写
-l     把输入全部转换成小写
如果没有命令行参数,则让程序像是使用了-p参数那样运行。

在这里插入代码片
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值