赞
踩
目录
5.下面的练习涉及字符串、循环、指针和递增指针。首先,假设定义了下面的函数:
考虑下面的函数调用: x = pr("Ho Ho Ho!");
sign占用多少字节的内存?'$'占用多少字节的内存?"$"占用多少字节的内存?
9.本章定义的s_gets()函数,用指针表示法代替数组表示法便可减少一个变量i。请改写该函数
10.strlen()函数接受一个指向字符串的指针作为参数,并返回该字符串的长度。请编写一个这样的函数
11.本章定义的s_gets()函数,可以用strchr()函数代替其中的while循环来查找换行符。请改写该函数
12.设计一个函数,接受一个指向字符串的指针,返回指向该字符串第1个空格字符的指针,或如果未找到空格字符,则返回空指针
13.重写程序清单11.21,使用ctype.h头文件中的函数,以便无论用户选择大写还是小写,该程序都能正确识别答案
1.设计并测试一个函数,从输入中获取下n个字符(包括空白、制表符、换行符),把结果储存在一个数组里,它的地址被传递作为一个参数
2.修改并编程练习1的函数,在n个字符后停止,或在读到第1个空白、制表符或换行符时停止,哪个先遇到哪个停止。不能只使用scanf()
3.设计并测试一个函数,从一行输入中把一个单词读入一个数组中,并丢弃输入行中的其余字符。该函数应该跳过第1个非空白字符前面的所有空白。将一个单词定义为没有空白、制表符或换行符的字符序列
4.设计并测试一个函数,它类似编程练习3的描述,只不过它接受第2个参数指明可读取的最大字符数
9.编写一个函数,把字符串中的内容用其反序字符串代替。在一个完整的程序中测试该函数,使用一个循环给函数提供输入值
10.编写一个函数接受一个字符串作为参数,并删除字符串中的空格。在一个程序中测试该函数,使用循环读取输入行,直到用户输入一行空行。该程序应该应用该函数只每个输入的字符串,并显示处理后的字符串
12.编写一个程序,读取输入,直至读到 EOF,报告读入的单词数、大写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h头文件中的函数。
13.编写一个程序,反序显示命令行参数的单词。例如,命令行参数是 see you later,该程序应打印later you see。
14.编写一个通过命令行运行的程序计算幂。第1个命令行参数是double类型的数,作为幂的底数,第2个参数是整数,作为幂的指数。
15.使用字符分类函数实现atoi()函数。如果输入的字符串不是纯数字,该函数返回0。
16.编写一个程序读取输入,直至读到文件结尾,然后把字符串打印出来。该程序识别和实现下面的命令行参数:
- int main(void)
- {
- char name[] = {'F', 'e', 's', 's' };
- ...
- }
没有终结符'\0',不是一个字符串,是一个字符数组
- #include <stdio.h>
- int main(void)
- {
- char note[] = "See you at the snack bar.";
- char *ptr;
- ptr = note;
- puts(ptr);
- puts(++ptr);
- note[7] = '\0';
- puts(note);
- puts(++ptr);
- return 0;
- }
See you at the snack bar.
ee you at the snack bar.
See you
e you//前面++了一次
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char food [] = "Yummy";
- char *ptr;
- ptr = food + strlen(food);
- while (--ptr >= food)
- puts(ptr);
- return 0;
- }
y
my
mmy
ummy
Yummy
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char goldwyn[40] = "art of it all ";
- char samuel[40] = "I read p";
- const char * quote = "the way through.";
- strcat(goldwyn, quote);
- strcat(samuel, goldwyn);
- puts(samuel);
- return 0;
- }
I read part of it all the way through.
- #include <stdio.h>
- char *pr(char *str)
- {
- char *pc;
- pc = str;
- while (*pc)
- putchar(*pc++);
- do
- {
- putchar(*--pc);
- }while (pc - str);
- return (pc);
- }
a. Ho Ho Ho!!oH oH oH
b. char*
c. 第一个H的地址
d. 当前指针所指元素的下一个元素的值;后者的值为当前元素的值 - 1
//*--pc的意思是把指针递减1,并使用存储在其位置上的值。--*pc指解引用pc所指向的值,然后把该值减一
e. Ho Ho Ho! !oH oH o
//!之间会有一个空字符,一般不产生打印效果,但我所用的编译器打印了个空格- -
f. 检测pc是否指向字符串的空字符
g. 第一个循环检测为空不执行,第二个循环pc递减指向空字符前面的存储区,并把其中的内容当做字符打印,此后pc将不会等于str,这个循环过程将一直持续
h. 声明pr(): char* pr(char*);
- char型变量占一个字节
- 字符型常量一般当做int型存储,一般是2或4个字节,我所用的编译器是4个字节
- 字符串占2个字节,除了'$'还有一个空字符占一个字节
- #include <stdio.h>
- #include <string.h>
- #define M1 "How are ya, sweetie? "
- char M2[40] = "Beat the clock.";
- char * M3 = "chat";
- int main(void)
- {
- char words[80];
- printf(M1);
- puts(M1);
- puts(M2);
- puts(M2 + 1);
- strcpy(words, M2);
- strcat(words, " Win a toy.");
- puts(words);
- words[4] = '\0';
- puts(words);
- while (*M3)
- puts(M3++);
- puts(--M3);
- puts(--M3);
- M3 = M1;
- puts(M3);
- return 0;
- }
//直接编译打印的- -
How are ya, sweetie? How are ya, sweetie?
Beat the clock.
eat the clock.
Beat the clock. Win a toy.
Beat
chat
hat
at
t
t
at
How are ya, sweetie?
- #include <stdio.h>
- int main(void)
- {
- char str1 [] = "gawsie";
- char str2 [] = "bletonism";
- char *ps;
- int i = 0;
- for (ps = str1; *ps != '\0'; ps++)
- {
- if (*ps == 'a' || *ps == 'e')
- putchar(*ps);
- else
- (*ps)--;
- putchar(*ps);
- }
- putchar('\n');
- while (str2[i] != '\0')
- {
- printf("%c", i % 3 ? str2[i] : '*');
- ++i;
- }
- return 0;
- }
faavrhee
//a和e要输出两次
*le*on*sm
- char* s_gets(char* st, int n)
- {
- char* ret_val;
- int i = 0;
- 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;
- }
- int sl(char* st)
- {
- int n = 0;
- while(*st++)
- {
- n++;
- }
- return n;
- }
- char* s_gets(char* st, int n)
- {
- char* ret_val;
- int i = 0;
- ret_val = fgets(st, n, stdin);
- if (ret_val)
- {
- char* f = strchr(st, '\n');
- if (f)
- *f = '\0';
- else
- while (getchar() != '\n')
- continue;
- }
- return ret_val;
- }
- char* fs(char* st)
- {
- while(*st++)
- {
- if(*st == ' ')
- {
- return st;
- }
- }
- return NULL;
- }
- #include <stdio.h>
- #include <string.h> // strcmp()函数的原型在该头文件中
- #include <ctype.h>
- #define ANSWER "grant"
- #define SIZE 40
- char * s_gets(char * st, int n);
-
- char* tl(char* a)
- {
- char* b = a;
- while(*a)//不能写*a++或者*++a, 其效果都是递增一位再解引用
- {
- *a = tolower(*a);
- a++;
- }
- return b;
- }
-
- int main(void)
- {
- char try[SIZE];
- puts("Who is buried in Grant's tomb?");
- s_gets(try, SIZE);
- while (strcmp(tl(try), ANSWER) != 0)
- {
- puts(try);
- puts("No, that's wrong. Try again.");
- s_gets(try, SIZE);
- }
- puts("That's right!");
- return 0;
- }
-
- char * s_gets(char * st, int n)
- {
- char * ret_val;
- int i = 0;
- ret_val = fgets(st, n, stdin);
- if (ret_val)
- {
- while (st[i] != '\n' && st[i] != '\0')
- i++;
- if (st[i] == '\n')
- st[i] = '\0';
- else
- while (getchar() != '\n')
- continue;
- }
- return ret_val;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- void func(char* a, int n)
- {
- int i;
- for (i = 0; i < n; ++i)
- {
- a[i] = getchar();
- }
- a[i] = '\0';
- }
-
- int main(void)
- {
- int n;
- scanf("%d", &n);
- char a[n + 1];
- func(a, n);
- puts(a);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- void func(char* a, int n)
- {
- int i;
- for (i = 0; i < n; ++i)
- {
- a[i] = getchar();
- if(a[i] <= ' ')
- {
- break;
- }
- }
- a[i] = '\0';
- }
-
- int main(void)
- {
- int n;
- scanf("%d", &n);
- getchar();
- char a[n + 1];
- func(a, n);
- puts(a);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- void func(char* a)
- {
- char ch;
- while ((ch = getchar()) <= ' ')
- {
- continue;
- }
- while(ch > ' ')//不能在这里写个getchar,第一次循环时已经读入了一个非空白符
- {
- *a = ch;
- a++;
- ch = getchar();
- }
- }
-
- int main(void)
- {
- char a[101];
- func(a);
- puts(a);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- void func(char* a, int n)
- {
- char ch;
- int i = 0;
- while ((ch = getchar()) <= ' ')
- {
- continue;
- }
- while(ch > ' ' && i < n)//不能在这里写个getchar,第一次循环时已经读入了一个非空白符
- {
- *a = ch;
- i++;
- a++;
- ch = getchar();
- }
- }
-
- int main(void)
- {
- int n;
- scanf("%d", &n);
- char a[n + 1];
- func(a, n);
- puts(a);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- char* func(char* a, char b)
- {
- while (*a)
- {
- if(*a == b)
- {
- return a;
- }
- a++;
- }
- return NULL;
- }
-
- int main(void)
- {
- char ch = getchar();
- getchar();
- char a[101];
- gets(a);
- char* x = func(a, ch);
- if(x)
- {
- putchar(*x);
- }
- else
- {
- puts("NONE");
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
-
- int is_within(char* a, char b)
- {
- while(*a)
- {
- if(*a == b)
- {
- return 1;
- }
- a++;
- }
- return 0;
- }
-
- int main()
- {
- char b, a[101];
- while(1)
- {
- b = getchar();
- getchar();
- gets(a);
- if(is_within(a, b))
- {
- puts("Y");
- }
- else
- {
- puts("N");
- }
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
-
- void mystrncpy(char *a, char *b, int n)
- {
- while(*a && n--)
- {
- *b = *a;
- a++;
- b++;
- }
- if(n)
- {
- *b = '\0';
- }
- }
-
- int main()
- {
- char b[101], a[101];
- int n;
- while(1)
- {
- scanf("%d", &n);
- gets(a);
- mystrncpy(a, b, n);
- puts(b);
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- void get_nextval(const char* t, int a[], int n)
- {
- int i = 0, j = -1;
- a[0] = -1;
- /*
- memset(a, -1, sizeof(int) * n);
- printf("%c%3c |", 'i', 'j');
- for (int k = 0; k < n; ++k) {
- printf("%3c", t[k]);
- }
- printf(" |%3c%3c\n", 'i', 'j');
- printf("--------------------------------\n");
- */
- while(i < n)
- {
- //printf("%d%3d |", i, j);
- if(j == -1 || t[i] == t[j])
- {
- ++i;
- ++j;
- if(t[i] != t[j])
- {
- a[i] = j;
- }
- else
- {
- a[i] = a[j];
- }
- }
- else
- {
- j = a[j];
- }
- /*
- for (int k = 0; k < n; ++k) {
- printf("%3d", a[k]);
- }
- printf(" |%3d%3d", i, j);
- putchar('\n');
- */
- }
- }
-
- char* string_in(char* s1, char* s2)//顺带复习KMP了- -
- {
- int n2 = 0, n1 = 0;
- char* a = s2;
- while(*a)
- {
- n2++;
- a++;
- }
- a = s1;
- while(*a)
- {
- n1++;
- a++;
- }
- int next[n2];
- get_nextval(s2, next, n2);
- int i = 0, j = -1;
- while(i < n1 && j < n2)
- {
- if(j == -1 || s1[i] == s2[j])
- {
- ++i;
- ++j;
- }
- else
- {
- j = next[j];
- }
- }
- if(j == n2)
- {
- return &s1[i - n2];
- }
- return NULL;
- }
-
- int main(void)
- {
- char s1[101], s2[101];
- gets(s1);
- gets(s2);
- char* x = string_in(s1, s2);
- if(x)
- {
- putchar(*x);
- }
- else
- {
- puts("N");
- }
- return 0;
- }
- /*
- ababcabcabdab
- abcabd
- */
- #include <stdio.h>
- #include <stdlib.h>
-
- void vers(char *a)
- {
- int n = 0;
- char* b = a;
- while(*b)//不能直接用a计算,后面要用a,不能改变a的值-_-||
- {
- n++;
- b++;
- }
- char t;
- int x = n / 2;//同上,不能写n /= 2 -_-||
- for(int i = 0; i < x; i++)
- {
- t = *(a + i);
- *(a + i) = *(a + n - 1 - i);
- *(a + n - 1 - i) = t;
- }
- }
-
- int main()
- {
- char a[101];
- while(1)
- {
- gets(a);
- vers(a);
- puts(a);
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
-
- int ds(char *a)
- {
- char *b = a;
- int n = 0;
- while (*b)
- {
- n++;
- b++;
- }
- char c[n + 1];
- char *d = c;
- b = a;
- while(*b)
- {
- if(*b == ' ')
- {
- b++;
- }
- else
- {
- *d = *b;
- b++;
- d++;
- }
- }
- *d = '\0';
- b = a;
- d = c;
- while (*d)
- {
- *b = *d;
- b++;
- d++;
- }
- *b = '\0';
- if(*a)
- {
- return 1;
- }
- return 0;
- }
-
- int main()
- {
- char a[101];
- int f = 1;
- while(f)
- {
- gets(a);
- f = ds(a);
- puts(a);
- }
- return 0;
- }
- // a b c d e f g h
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- void strprint(char *a[])
- {
- for (int i = 0; i < 10; ++i) {
- puts(a[i]);
- }
- }
-
- void ASCIISort(char *a[])
- {
- char *t;
- for (int i = 0; i < 9; ++i) {
- for (int j = i + 1; j < 10; ++j) {
- if(strcmp(a[i], a[j]) > 0)
- {
- t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- }
- }
- strprint(a);
- }
-
- void LengthSort(char *a[])
- {
- char *t;
- for (int i = 0; i < 9; ++i) {
- for (int j = i + 1; j < 10; ++j) {
- if(strlen(a[i]) > strlen(a[j]))
- {
- t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- }
- }
- strprint(a);
- }
-
- int FWcmp(char *a, char *b)
- {
- int an = 0, bn = 0;
- while(*a && !isalpha(*a))
- {
- a++;
- }
- while(*a && isalpha(*a))
- {
- a++;
- an++;
- }
- while(*b && !isalpha(*b))
- {
- b++;
- }
- while(*b && isalpha(*b))
- {
- b++;
- bn++;
- }
- if(an > bn)
- {
- return 1;
- }
- return 0;
- }
-
- void FWLengthSort(char *a[])
- {
- char *t;
- for (int i = 0; i < 9; ++i) {
- for (int j = i + 1; j < 10; ++j) {
- if(FWcmp(a[i], a[j]))
- {
- t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- }
- }
- strprint(a);
- }
-
- void pro(char **a)
- {
- int n;
- puts("input from 1 to 5 to select from the followed options:");
- puts("1)print 2)ASCII");
- puts("3)Length 4)FWLength");
- puts("5)quit");
- while(~scanf("%d", &n) && n != 5)
- {
- switch (n)
- {
- case 1:
- strprint(a);
- case 2:
- ASCIISort(a);
- break;
- case 3:
- LengthSort(a);
- break;
- case 4:
- FWLengthSort(a);
- break;
- default:
- break;
- }
- }
- }
-
- int main()
- {
- FILE* in = fopen("F:\\C_Codes\\draft\\123.txt", "r");
- char a[10][101];
- for (int i = 0; i < 10; ++i) {
- fgets(a[i], 100, in);
- }
- char *b[10];
- for (int j = 0; j < 10; ++j) {
- b[j] = a[j];
- }
- pro(b);
- return 0;
- }
怎么设置形参这里试验出现了几个问题:
- char (*a)[] 这样设置形参无法通过赋值传地址,会出现类型不匹配,也不能 a + i = d 这样赋值,是个非法表达式
- char **a 无法通过 a + i 或 a[i] 来取得字符串首地址,编译器识别为地址的地址,而不是二维数组
所以,选择使用传入复制的地址数组,通过改变地址数组的排序达到排序的目的
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- int main()
- {
- FILE* in = fopen("F:\\C_Codes\\draft\\123.txt", "r");
- char ch;
- int un = 0, ln = 0, wn = 0, pn = 0, nn = 0, f = 0;
- while((ch = getc(in)) != EOF)
- {
- if(isupper(ch))
- {
- un++;
- f = 1;
- }
- else if(islower(ch))
- {
- ln++;
- f = 1;
- }
- else if(ispunct(ch))
- {
- pn++;
- if(f)
- {
- wn++;
- f = 0;
- }
- }
- else if(isdigit(ch))
- {
- nn++;
- }
- }
- printf("%d %d %d %d %d\n", wn, un, ln, pn, nn);
- fclose(in);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- int main(int argc, char** argv)
- {
- for (int i = argc - 1; i > 0 ; --i) {
- printf("%s ", argv[i]);
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- int main(int argc, char** argv)
- {
- char *end;
- double a = strtod(argv[1], &end);
- long b = strtol(argv[2], &end, 10);
- double sum = 1.0;
- for (long i = 0; i < b; ++i) {
- sum *= a;
- }
- printf("%lf\n", sum);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- int myatoi(char *a)
- {
- int n = 0;
- int len = 0;
- while(*a)
- {
- n *= 10;
- if(!isdigit(*a))
- {
- return 0;
- }
- n += *a - '0';
- a++;
- }
- return n;
- }
-
- int main()
- {
- char a[101];
- gets(a);
- printf("%d\n", myatoi(a));
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- int main(int argc, char **argv)
- {
- int n;
- if(argv[1][1] == 'p' || argc == 1)
- {
- n = 1;
- }
- else if(argv[1][1] == 'u')
- {
- n = 2;
- }
- else if(argv[1][1] == 'l')
- {
- n = 3;
- }
- FILE* in = fopen("F:\\C_Codes\\draft\\123.txt", "r");
- char ch;
- while ((ch = fgetc(in)) != EOF)
- {
- if(n == 1)
- {
- putchar(ch);
- }
- else if(n == 2)
- {
- if(isalpha(ch))
- {
- putchar(toupper(ch));
- continue;
- }
- putchar(ch);
- }
- else
- {
- if(isalpha(ch))
- {
- putchar(tolower(ch));
- continue;
- }
- putchar(ch);
- }
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。