C primer plus 编程练习 11.13

2.

#include <stdio.h>
#define LEN 10
char * getnchar(char *,int );

int main(void)
{
	char intput[LEN];
	char *check;

	printf("please input:\n");
	check = getnchar(intput,LEN - 1);
	if (check == NULL)
	    puts("Input failed");
	else
	    puts(intput);
    puts("Done!\n");
	return 0;
}
char * getnchar(char *str,int n )
{
	int i;
	int ch;
	for (i = 0;i <n;i++)
	{
		ch = getchar();
		if (ch != EOF && ch != '\n')
			str[i] = ch;
		else
			break;	
	}
	if (ch == EOF)
	    return NULL;
	else
	{
		str[i] = '\0';
		return str;
	}
}

3.

#include <stdio.h>
#include <ctype.h>
#define LEN 10
char * getword(char * str);
int main(void)
{
    char input[LEN];
    while (getword(input) != NULL)
        puts(input);
    puts("Done.\n");
    return 0;
}

char * getword(char * str)
{
    int ch;
    char * orig = str;
    // skip over initial whitespace
    while ((ch = getchar()) != EOF && isspace(ch))
        continue;
    if (ch == EOF)
        return NULL;
    else
        *str++ = ch; // first character in word
    // get rest of word
    while ((ch = getchar()) != EOF && !isspace(ch))
        *str++ = ch;
    *str = '\0';
    if (ch == EOF)
        return NULL;
    else
    {
        while (ch != '\n')
            ch = getchar();
        return orig;
    }
}

5.

#include <stdio.h>
#include <string.h>
char * char_search(char * str,char ch);

int main(void)
{
	char str[] = "ni hao dajia ajksdj asf";
	char * find;
	char ch;
	printf("请输入您要查找的字符\n");
	while (scanf("%c",&ch) != EOF)
	{
	    find = char_search(str,ch);
	    if (find)
	    {
	    	printf("字符串首地址为%p\n,末尾地址为%p\n",str,str + strlen(str));
            printf("你查找的字符%c在字符串中的位置为%p\n",ch,find);
	    }
	    else
	    	puts("没找到");
	    printf("请输入您要查找的字符\n");
	    }
	return 0;
}
char * char_search(char * str,char ch)
{
    while (ch != *str && *str != '\0')
    {
		str++;
		continue;
    } 
    if (*str == '\0')
		return NULL;
    else
		return str;
}

7.

#include <stdio.h>
#include <string.h>
#define LEN1 60
#define LEN2 20
char * s_gets(char * st, int n);
char * mystrncpy(char *s1,char *s2, int n);
int main(void)
{
	char s2[LEN2];
	char s1[LEN1];
	int n;
	printf("请输入您要拷贝的字符串:\n");
	while (s_gets(s2, LEN2) && s2[0] != '\0')
	{
        printf("您输入的字符串是:%s\n",s2);
        puts("请输入您要拷贝的字符数:");
		while(scanf("%d",&n) != 1)
			continue;
		getchar();
        mystrncpy(s1,s2,n);
		printf("拷贝后s1中内容为:\n");
		puts(s1);
        printf("请输入下一个您要拷贝的字符串:\n");
	}
	return 0;
}
char * mystrncpy(char *s1,char *s2, int n)
{
	int i = 0;
	while (s2[i] != '\0' && i < n)
	{
		s1[i] = s2[i];
		i++;
	}
	s1[i] = '\0';
    return s1;
}
char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n'); // look for newline
        if (find) // if the address is not NULL,
            *find = '\0'; // place a null character there
        else
            while (getchar() != '\n')
               continue;
    }
    return ret_val;
}

9.

#include <stdio.h>
#define LEN 80
char * s_gets(char * st, int n);
char * reverse_order(char * str);

int main(void)
{
	char str[LEN];
	printf("请输入你要处理的字符串\n");
	while (s_gets(str, LEN) && str[0] != '\0')
	{
		reverse_order(str);
		puts("逆序排列后的字符串为");
		puts(str);
		printf("请输入下一个要处理的字符串\n");
	}

	return 0;
}
#include <string.h>
char * reverse_order(char * str)
{
	char temp;
	int i;
	int n = strlen(str);
	for (i = 0;i < n / 2;i++ )
	{
        temp = str[i];
		str[i] = str[n - i - 1];
		str[n - i - 1] = temp;
	}
	return str;
}
char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n'); // look for newline
        if (find) // if the address is not NULL,
            *find = '\0'; // place a null character there
        else
            while (getchar() != '\n')
               continue;
    }
    return ret_val;
}

 

11.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LIM 10           //可以读入的最大行数
#define SIZE 40          //限制字符串长度
char * s_gets(char * st, int n);   //读取字符串,将换行符换成空字符
int menu(void);       //显示菜单,并返回选择项
void showstr2(char str[][SIZE],int n);    //显示原始的字符串数组
void stsrt(char *strings [],int num);    //按ASCII码排序字符串
void stsrt2(char *strings [],int num);    //按字符串长度排序字符串
void stsrt3(char *strings [],int num);   //按第一个单词长度排序字符串
void showstr_sort1(char * str [],int n);   //显示排序后的字符串(通过指针数组)
int firstwordlenth(char * str);        //计算第一个单词长度
int main(void)
{
	char str[LIM][SIZE];   //储存输入的字符串
	char *ptr[LIM];        //指针数组,每个元素指向一个字符串
	int ct;               //输入计数
	int choice;           //用户选择结果
	char * check;        //输入检查指针
    
        puts("请输入10个字符串");
	for (ct = 0;ct < LIM;ct++)
	{
		printf("第%d个字符串: ",ct + 1);

		check = s_gets(str[ct],LIM);
		ptr[ct] = str[ct];
		if (check == NULL)
		    break;              
		if (str[ct][0] == '\0')    //输入空行提示重新输入
		{
			printf("不能输入空行,请重新第%d个字符串\n",ct);
			ct--;
		}
	}
	while ((choice = menu()) != 5)
	{
		switch (choice)
		{
		case 1:
			puts("源字符串列表:");
			showstr2(str,LIM);
			break;
		case 2:
			puts("按ASCII码顺序打印:");
		    stsrt(ptr,ct);
			showstr_sort1(ptr,LIM);
			break;
		case 3:
			puts("按长度打印为:");
		    stsrt2(ptr,ct);
			showstr_sort1(ptr,LIM);
			break;
		case 4:
			puts("按第一个单词长度打印:");
		    stsrt3(ptr,ct);
			showstr_sort1(ptr,LIM);
			break;
		}
	}
	
        printf("再见!\n");
	return 0;
}
int firstwordlenth(char * str)
{
	int lenth = 0;
	int i =0;
	while (isspace(str[i]))   //跳过第一个单词前面的空白字符
		i++;
	if (str[i] == '\0')    //如果没有单词就返回0
	    return 0;
	while (!isspace(str[i]) && str[i] != '\0')
	{
	    i++;
	    lenth++;         //统计单词数量
	}
	return lenth;
}
void stsrt3(char *strings [],int num)
{
    char *temp;
	int top,seek;
	for (top = 0;top < num - 1;top++)
	    for (seek = top + 1;seek < num;seek++)
	        if(firstwordlenth(strings[top]) > firstwordlenth(strings[seek]))
	        {
		        temp = strings[top];
			    strings[top] = strings[seek];
			    strings[seek] = temp;
	        }
}
void stsrt2(char *strings [],int num)
{
    char *temp;
	int top,seek;
	for (top = 0;top < num - 1;top++)
	    for (seek = top + 1;seek < num;seek++)
	        if(strlen(strings[top]) > strlen(strings[seek]))
	        {
		        temp = strings[top];
			    strings[top] = strings[seek];
			    strings[seek] = temp;
	        }
}
void showstr_sort1(char * str [],int n)
{
	int i;
	for (i = 0;i < n;i++)
	    puts(str[i]);     
}
void stsrt(char *strings [],int num)
{
	char *temp;
	int top,seek;
	for (top = 0;top < num - 1;top++)
	    for (seek = top + 1;seek < num;seek++)
	        if(strcmp(strings[top],strings[seek]) > 0)
	        {
		        temp = strings[top];
			    strings[top] = strings[seek];
			    strings[seek] = temp;
	        }
}
void showstr2(char str[][SIZE],int n)
{
	int i;
	for (i = 0;i < n;i++ )
        puts(str[i]);
}
int menu(void)
{
	int choice = 5;
	int status;
	puts("**********************************");
	puts("1.打印源字符串列表");
	puts("2.以ASCII顺序打印字符串");
	puts("3.按长度递增打印字符串");
	puts("4.按字符串中第1个单词长度打印字符串");
	puts("5.退出");
	puts("**********************************");
	puts("请输入您的选择(数字1-5)");
	while((status = scanf("%d",&choice)) != 1 || (choice < 1 || choice > 5))
	{
		puts("请输入数字1-5");
        if(status != 1)
			while(getchar() != '\n')
			    continue;
	}
	return choice;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n'); // look for newline
        if (find) // if the address is not NULL, 
            *find = '\0'; // place a null character there
        else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

12

/* pe11-12.c -- counts words and certain characters */
/* Programming Exercise 11-11 */
#include <stdio.h>
#include <ctype.h> // for isspace()
#include <stdbool.h> // for bool, true, false
int main(void)
{
char c; // read in character
int low_ct = 0; // number of lowercase characters
int up_ct = 0; // number of uppercase characters
int dig_ct = 0; // number of digits
int n_words = 0; // number of words
int punc_ct = 0; // number of punctuation marks
bool inword = false; // == true if c is in a word
printf("Enter text to be analyzed (EOF to terminate):\n");
while ((c = getchar()) != EOF)
{
    if (islower(c))
       low_ct++;
    else if (isupper(c))
       up_ct++;
    else if (isdigit(c))
       dig_ct++;
    else if (ispunct(c))
       punc_ct++;
    if (isalpha(c) && !inword)
    {
        inword = true; // starting a new word
        n_words++; // count word
    }
    if (!isalpha(c) && inword)
        inword = false; // reached end of word
    }
    printf("\nwords = %d, lowercase = %d, uppercase = %d, "
        "digits = %d, punctuation = %d\n",
        n_words,low_ct,up_ct, dig_ct, punc_ct);
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值