C Prime Plus ch11练习题

这篇博客主要涵盖了C语言的第11章复习,包括了11-9到11-13的复习题及11.1至11.16的编程题。内容涉及使用fgets(), gets(), scanf(), getchar()获取字符,强调了getchar()在处理空格、制表符和换行符时的便捷性。同时,讨论了在不同循环方式下,如何结合字符串知识点进行编程,并指出某些题目在输出处理上的特定要求。" 130251181,18090643,Qt布局管理:Grid与Form Layout详解,"['Qt', 'GUI开发', '布局']
摘要由CSDN通过智能技术生成

复习题:

11-9

char* s_gets(char* st, int n)
{
    char* ret_val;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            *st = '\0';
        else
        {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

11-10

int strlen(char* pt)
{
    int i = 0;
    while (*pt)
        i++;
    return i;
}

11-11

char* s_gets(char* pt, int n)
{
    char* ret_val;
    char* end;
    ret_val = fgets(pt, n, stdin);
    if (ret_val)
    {
       end=strchr(pt, '\n');
       if (end)
           *end = '\0';
       while (getchar() != '\n')
           continue;
    }
}

11-12

char* find_space(char* st)
{
    char* ret_val;
    char* find;
    ret_val = fgets(st, 81, stdin);
    if (ret_val)
    {
        while (*st != ' ' && *st != '\0')
            st++;
        if (*st == ' ')
            find = st;
        else
        {
            find = NULL;
        }

    }
    return find;
}

11-13

int main(void)
{
    char input[81];
    char* ret_val;
    puts("Who's buried in GRANT's tomb?");
    ret_val=s_get(input, 81);
        Tolower(input);
        while (strcmp(input, ANSWER) != 0) //如有有\n符,也会比较
        {
            printf("No,that's wrong.Try again.\n");
            ret_val = s_get(input, 81);
            Tolower(input);
        }
            puts("That's right.");
            
    return 0;
}
void Tolower(char* pt)
{
    while (*pt)
    {
        *pt = tolower(*pt);
        pt++;
    }
}

char* s_get(char* pt, int n)
{
    char* ret_val;
    ret_val = fgets(pt, n, stdin);
    if (ret_val)
    {
        while (*pt != '\n' && *pt != '\0')
            pt++;
        if (*pt == '\n')
            *pt = '\0';
        else
        {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

编程题:

11.1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81
char* Get_char(char* pt,int n);
int main()
{
    int length;
    char input[MAX];
    char* pt;

    puts("Enter your string's length(less than 81)");
    scanf("%d", &length);
    puts("Enter your string");
    pt = Get_char(input, length);
    puts(input);
  
    return 0;
}

char* Get_char(char* pt,int n)
{
    char* ret_val;
    int i = 0;

    while (i < n )
    {
        pt[i] = getchar();
        i++;
    }
    pt[n] = '\0';
    ret_val = pt;
    return  ret_val;
}

从输入中获取字符,除了fgets(),gets(),还有scanf() 和 getchar(),如果要获取空格、制表符和换行符,除了fgets(),还可以用getchar(),并且这个更方便。

11.2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81
#define BLANK ' '
#define ENTER '\n'
char* Get_char(char* pt,int n);
int main()
{
    int length;
    char input[MAX];
    char* pt;

    puts("Enter your string's length(less than 81)");
    scanf("%d", &length);
    getchar();
    puts("Enter your string");
    pt = Get_char(input, length);
    puts(input);
  
    return 0;
}

char* Get_char(char* pt,int n)
{
    char* ret_val;
    int i = -1;

    do
    {
        i++;
        pt[i] = getchar();
    } while (i < n && pt[i] != BLANK && pt[i] != ENTER);
    pt[i] = '\0';
    ret_val = pt;
    return  ret_val;
}

循环方式也有多种,字符串这章一定要把前边学的所有知识都用上

11.3

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81

char* Get_word(char* pt);
int main()
{
    char input[MAX];
    char* pt;

    puts("Enter your word:(enter to quit)");
    pt = Get_word(input);
    puts(input);
  
    return 0;
}

char* Get_word(char* pt)
{
    char* ret_val;
    scanf("%s", pt);
    ret_val = pt;
    while ((getchar()) != '\n')
        continue;
    return  ret_val;
}

11.4

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81

char* Get_word(char* pt,int n);
int main()
{
    char input[MAX];
    char* pt;
    int length;

    puts("Enter your length");
    scanf("%d", &length);
    getchar();
    puts("Enter your word:(enter to quit)");
    pt = Get_word(input,length);
    puts(input);
  
    return 0;
}

char* Get_word(char* pt,int n)
{
    char* ret_val;
    ret_val = pt;
    int j = 0;
    scanf("%s", pt);
    while (j < n)
    {
        j++; 
        pt++;
    }
    *pt = '\0';
    while ((getchar()) != '\n')
        continue;
    return  ret_val;
}

11.5

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81

char* Search(char* source, char ch);
int main()
{
    char input[MAX];
    char* pt;
    char aim;

    puts("Enter the char you want to look for");
    scanf("%c", &aim);
    while (aim != '\n')
    {
        getchar();
        puts("Enter your word:)");
        fgets(input, MAX, stdin);
        pt = Search(input, aim);
        if (pt)
            printf("Find the %c in %s.\n",aim,input);
        else
            puts("Not found.");
        puts("Enter the char you want to look for(enter to quit)");
        scanf("%c", &aim);
    }
    puts("Done");
    
    return 0;
}

char* Search(char* source,char ch )
{
    char* ret_val;
    ret_val = NULL;
    while ((*source) !='\n')
    {
        if (( * source) == ch)
            ret_val = source;
        source++;
    }
    return  ret_val;
}

11.6

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81

int is_within(char ch, char* pt);
int main()
{
    char input[MAX];
    char aim;
    int i;

    puts("Enter the char you want to look for");
    scanf("%c", &aim);
    while (aim != '\n')
    {
        getchar();
        puts("Enter your word:)");
        fgets(input, MAX, stdin);
        i = is_within(aim, input);
        if (1)
            printf("Find the %c in %s.\n",aim,input);
        else
            puts("Not found.");
        puts("Enter the char you want to look for(enter to quit)");
        scanf("%c", &aim);
    }
    puts("Done");
    
    return 0;
}

int is_within(char ch, char* pt)
{
    int flag = 0;
    int i = 0;

    while (*pt != '\n')
    {
        if (*pt == ch)
            flag = 1;
        pt++;
    }
    return flag;
}

11.7

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81
char* mystrncpy(char* pt1, char* pt2, int n);
char* s_gets(char* pt, int n);

int main()
{
    char input1[MAX];
    char input2[MAX];
    int n;

    puts("Enter the number you want to copy:");
    ;
    while (scanf("%d", &n) == 1)
    {
        getchar();
        puts("Enter the first string:");
        s_gets(input1, MAX);
        puts("Enter the second string:");
        s_gets(input2, MAX);
        mystrncpy(input1, input2, n);
            puts(input1);
        puts("Enter the number you want to copy(q to quit):");
   }
       
    
    puts("Done");
    
    return 0;
}

char * mystrncpy(char* pt1, char* pt2, int n)
{
    int i = 0;
    int j = strlen(pt1);
    char* ret_val;
    while (i < n && pt2[i] != '\0')
    {
        pt1[j] = pt2[i];
        j++;
        i++;
   }
    if (i < n)
    {
        while (i < n)
        {
            pt1[j] = '\0';
            i++;
        }
    }
    ret_val = pt1;
    return ret_val;
}
char * s_gets(char* pt, int n)
{
    char* ret_val;
    ret_val = fgets(pt, MAX, stdin);
        if (ret_val)
        {
            while (*pt != '\0' && *pt != '\n')
                pt++;
            if (*pt == '\n')
                *pt = '\0';
            else
            {
                while (getchar() != '\n')
                    continue;
            }
        }
        return  ret_val;
}

题目要求如果大于限定数量,不添加空字符,所以输出会有问题。

11.8

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81
char* s_gets(char* pt, int n);
char* string_in(char* source, char* target);

int main()
{
    char input1[MAX];
    char input2[MAX];
    char* pt;

    puts("Enter the first string:");
    s_gets(input1, MAX);
    while (*input1 != '\0')
    {
        puts("Enter the second string:");
        s_gets(input2, MAX);
        pt = string_in(input1, input2);
        if (pt)
        {
            printf("The second string is in the first string,and it is in %p.\n", pt);
        }
        else
            puts("The second string is not in the first string");

        puts("Enter the first string:(enter in the first line to quit)");
        s_gets(input1, MAX);
     }
    puts("Done");
    
    return 0;
}

char * string_in(char* source, char* target)
{
    char* ret_val;
    ret_val = NULL;
    while (*source)
    {
        if (*source == *target)
        {
            ret_val = source;
            source++;
            target++;
            while (*source == *target && *target != '\0' && *target != '\n')
            {
                source++;
                target++;
            }
            if (*target == '\0' || *target == '\n')
                continue;
            else
                ret_val = NULL;
        }
        source++;
    }
       return  ret_val;
}
char* s_gets(char* pt, int n)
{
    char* ret_val;
    ret_val = fgets(pt, n, stdin);
    if (ret_val)
    {
        while (*pt != '\n' && *pt != '\0')
            pt++;
        if (*pt == '\n')
            *pt = '\0';
        else
        {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

11.9

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81
char* s_gets(char* pt, int n);
char* string_reverse(char* source, char* storage);

int main()
{
    char input[MAX];
    char * storage[MAX];
    char* pt;

    puts("Enter the string:");
    s_gets(input, MAX);
    while (*input)
    {
        pt = string_reverse(input, storage);
        puts(pt);
        puts("Enter the string:(enter in the beginning of a line to quit)");
        s_gets(input, MAX);
    }
    puts("Done");
    return 0;
}

char * string_reverse(char* source, char* storage)
{
    int length;
    int i;
    length = strlen(source);
    *(storage + length) = '\0';
    for (i = 0; i < length; i++)
    {
        *(storage+length-i-1) = *(source+i);
    }
    return storage;
} 
     
char* s_gets(char* pt, int n)
{
    char* ret_val;
    ret_val = fgets(pt, n, stdin);
    if (ret_val)
    {
        while (*pt != '\n' && *pt != '\0')
            pt++;
        if (*pt == '\n')
            *pt = '\0';
        else
        {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

11.10

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81
#define BLANK ' '
char* SPACE_DELETE(char* source);

int main()
{
    char input[MAX];
    char *storage;
    int i = 0;
    int j = 0;

    puts("Enter the string you want to process");
    fgets(input, MAX, stdin);
    while (*input != '\n')
    {
        storage = SPACE_DELETE(input);
        puts(storage);
        puts("Enter the string you want to process");
        fgets(input, MAX, stdin);
    }
    puts("Done");
    return 0;
}

char * SPACE_DELETE(char* source)
{
    char *ret_val;
    ret_val = source;
    int i = 0;
    while (*source != '\n' )
    {
        if (*source != BLANK)
        source++;
        else 
            strcpy(source,source+1);  //拷贝更方便
    }
    return ret_val;
} 

11-11

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 81
#define BAR "***************************************************************************" 
void Show_menu(void);
char Get_choice(void);
char Get_first(void);
void Print_with_nochange(char pt[ ][MAX], int n);
void Print_with_ascii(char pt[][MAX], int n);
void Print_according_its_length(char pt[][MAX], int n);
void Print_its_first_word(char pt[][MAX], int n);

int main(void)
{
	char input[10][MAX];
	char* ret_val;
	int i = 0;
	char choice;
	puts("Enter your input:");
	do
	{
		ret_val=fgets(input[i], MAX, stdin);
		i++;
	} while (i < 10 && ret_val != NULL);
		Show_menu();
	choice = Get_choice();
	while (choice != 'q')
	{
		switch (choice)
		{
		case 'a':
			puts("结果是;");
			Print_with_nochange(input, 10);
			break;
		case 'b':
			puts("结果是;");
			Print_with_ascii(input, 10);
			break;
		case 'c':
			puts("结果是;");
			Print_according_its_length(input, 10);
			break;
		case 'd':
			puts("结果是;");
			Print_its_first_word(input, 10);
			break;
		case 'q':
			break;
		}
		Show_menu();
		choice = Get_choice();
	}
	puts("done");
	return 0;
}

void Show_menu(void)
{
	puts(BAR);
	puts("a.打印原字符表               b.以ASCII表中的顺序打印字符串");
	puts("c.按长度递增顺序打印字符串   d.按字符串中第一单词长度打印字符串");
	puts("q.退出");
	puts(BAR);
}

char Get_choice(void)
{
	char choice;

	puts("Please enter your choice:");
		choice = Get_first();
		while (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'q')
		{
			puts("Wrong input.Please try again.");
			choice = Get_first();
		}

		return choice;
}

char Get_first(void)
{
	char ch;
	while ((ch = getchar()) == '\n' || ch == ' ')
		continue;
	while (getchar() != '\n')
		continue;
	return ch;
}
void Print_with_nochange(char pt[ ][MAX], int n)
{
	int i=0;

	for (i = 0; i < n; i++)
		fputs(pt[i],stdout);
}
void Print_with_ascii(char pt[ ][MAX], int n)
{
	char temp[MAX];
	int i, j;
	int m = 0;
	
	for (i = 0; i < n - 1; i++)
	{
		for (j = i+1; j < n;j++)
		{
			if (strcmp(pt[i], pt[j]) > 0)
			{
				strcpy(temp, pt[i]);
				strcpy(pt[i], pt[j]);
				strcpy(pt[j], temp);
			}
		}
	}
	while (m < 10)
	{
		fputs(pt[m], stdout);
		m++;
	}
}
void Print_according_its_length(char pt[][MAX], int n)
{
	int num_1;
	int num_2;
	char temp[MAX];
	int i, j;
	int m = 0;

	for (i = 0; i < n - 1; i++)
	{
		for (j = i; j < n; j++)
		{
			num_1 = strlen(pt[i]);
			num_2 = strlen(pt[j]);
			if (num_2 < num_1)
			{
				strcpy(temp, pt[i]);
				strcpy(pt[i], pt[j]);
				strcpy(pt[j], temp);
			}
		}
	}
	while (m < 10)
	{
		fputs(pt[m], stdout);
		m++;
	}
}
void Print_its_first_word(char pt[][MAX], int n)
{
	int i;
	char* pointer;
	
	for (i = 0; i < n; i++)
	{
		pointer = pt[i];
		while (*pointer == ' ')
			pointer++;
		while (*pointer != ' '&& *pointer != '\n')
		{
			printf("%c", *pointer);
			pointer++;
		}
		printf("\n");
	}
}

11-12

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 81
int Count_Words(char* pt);
int Count_Up_Letter(char* pt);
int Count_Lower_Letter(char* pt);
int Count_Punc(char* pt);
int Count_Num(char* pt);

int main(void)
{
	char* input[MAX];
	char* pt;
	int count_words = 0;
	int count_up_letter = 0;
	int count_lower_letter = 0;
	int count_punc = 0;
	int count_num = 0;

	pt = input;
	puts("Enter your input:");
	while ((*pt = getchar()) != EOF)  //ctrl z单独的一行开头
	{
		pt++;
	 }
	*pt = '\0';
	 count_words = Count_Words(input);
	 count_up_letter = Count_Up_Letter(input);
	 count_lower_letter = Count_Lower_Letter(input);
	 count_punc = Count_Punc(input);
	 count_num = Count_Num(input);
	 printf("There are %d words.\n", count_words);
	 printf("There are %d up words.\n", count_up_letter);
	 printf("There are %d lower words.\n", count_lower_letter);
	 printf("There are %d punctions.\n", count_punc);
	 printf("There are %d digits.\n", count_num);
	return 0;
}

int Count_Words(char* pt)
{
	int count = 0;
	int flag = 0;
	while (*pt != '\0')
	{
		if (isalpha(*pt) == 0 && flag == 0)
			pt++;
		else if (isalpha(*pt) == 0 && flag == 1)
		{
			pt++;
			count++;
			flag = 0;
		}
		else if (isalpha(*pt) != 0)
		{
			pt++;
			flag = 1;
		}
	}
		if (flag == 1)
			count++;

	return count;
}
int Count_Up_Letter(char* pt)
{
	int count = 0;
	while (*pt != '\0')
	{
		if (isupper(*pt))
			count++;
		pt++;
	}
	return count;
}
int Count_Lower_Letter(char* pt)
{
	int count = 0;
	while (*pt != '\0')
	{
		if (islower(*pt))
			count++;
		pt++;
	}
	return count;
}
int Count_Punc(char* pt)
{
	int count = 0;
	while (*pt != '\0')
	{
		if (ispunct(*pt))
			count++;
		pt++;
	}
	return count;
}
int Count_Num(char* pt)
{

	int count = 0;
	while (*pt != '\0')
	{
		if (isdigit(*pt))
			count++;
		pt++;
	}
	return count;
}

11-13

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main(int argc,char *argv[])
{
	if (argc < 2)
		printf("Error!not enough parameter to display\n");
	else
	{
		for (int i = argc; i > 1; i--)
			printf("%s ", argv[i - 1]);
	}
	printf("\n");
	return 0;
}

11-14

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

int main(int argc,char *argv[])
{
	int zhishu;
	double dishu;
	double sum = 1;
	if (argc < 2)
		printf("Error!not enough parameter to display\n");
	else
	{
		dishu = atof(argv[1]);
		zhishu = atoi(argv[2]);
		while (zhishu > 0)
		{
			sum *= dishu;
			zhishu--;
		}
		printf("%lf", sum);
	}
	
	return 0;
}

11-15

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 81
int atoint(char* input);

int main(void)
{
	char input[MAX];
	int digit;
	fgets(input, MAX, stdin);
	digit = atoint(input);
	printf("%d", digit);
	return 0;
}

int atoint(char* input)
{
	char* pt;
	int beishu = 1;
	int sum = 0;
	int count = 0;
	pt = input;
	while (*pt != '\0')
	{
		pt++;
		count++;
	}
	while(count >= 0)
	{
		if (isdigit(*pt) == 0)
		{
			pt--;
			count--;
		}
		else if (isdigit(*pt) != 0)
		{
			sum = sum + (*pt - '0')*beishu;
			beishu *= 10;
			pt--;
			count--;
		}
	}
	return sum;
}

11-16

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 81
void To_Upper(char* input);
void To_Lower(char* input);

int main(int argc, char* argv[])
{
	char input[MAX];
	puts("Enter a string to convert:");
	fgets(input, MAX, stdin);
	char ch;                  //这里因为输入了字符串,参数不仅仅是个-p -u -l,所以不能用argc 
                              //或者argv去判断;
	puts("Enter your choice:");
	getchar();                //得到前边的负号
	ch = getchar();
		switch (ch)
		{
		case 'p':
			puts(input);
			break;
		case 'u':
			To_Upper(input);
			break;
		case 'l':
			To_Lower(input);
			break;
		}
	return 0;
}

void To_Upper(char* input)
{
	while (*input != '\0' && *input != EOF)
	{
		putchar(toupper(*input));
		input++;
	}

}
void To_Lower(char* input)
{
	while (*input != '\0' && *input != EOF)
	{
		putchar(tolower(*input));
		input++;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值