目录

 

复习题

1.下面字符串的声明有什么问题?

2.下面的程序会打印什么

3.下面的程序会打印什么

4.下面的程序会打印什么

5.下面的练习涉及字符串、循环、指针和递增指针。首先,假设定义了下面的函数:

考虑下面的函数调用: x = pr("Ho Ho Ho!");

a.将打印什么?

b.x是什么类型?

c.x的值是什么?

d.表达式*--pc是什么意思?与--*pc有何不同?

e.如果用*pc--替换*--pc,会打印什么?

f.两个while循环用来测试什么?

g.如果pr()函数的参数是空字符串,会怎样?

h.必须在主调函数中做什么,才能让pr()函数正常运行?

6.假设有如下声明:

char sign = '$';

sign占用多少字节的内存?'$'占用多少字节的内存?"$"占用多少字节的内存?

7.下面的程序会打印出什么

8.下面的程序会打印出什么

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个参数指明可读取的最大字符数

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

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

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

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

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

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参数那样运行


复习题

1.下面字符串的声明有什么问题?

int main(void)
{
    char name[] = {'F', 'e', 's', 's' };
    ...
}

2.下面的程序会打印什么

#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;
}

3.下面的程序会打印什么

#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;
}

4.下面的程序会打印什么

#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;
}

5.下面的练习涉及字符串、循环、指针和递增指针。首先,假设定义了下面的函数:

#include <stdio.h>
char *pr(char *str)
{
    char *pc;
    pc = str;
    while (*pc)
        putchar(*pc++);
    do 
    {
        putchar(*--pc);
    }while (pc - str);
    return (pc);
}

考虑下面的函数调用: x = pr("Ho Ho Ho!");

a.将打印什么?

b.x是什么类型?

c.x的值是什么?

d.表达式*--pc是什么意思?与--*pc有何不同?

e.如果用*pc--替换*--pc,会打印什么?

f.两个while循环用来测试什么?

g.如果pr()函数的参数是空字符串,会怎样?

h.必须在主调函数中做什么,才能让pr()函数正常运行?

6.假设有如下声明:

char sign = '$';

sign占用多少字节的内存?'$'占用多少字节的内存?"$"占用多少字节的内存?

7.下面的程序会打印出什么

#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;
}

8.下面的程序会打印出什么

#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;
}

9.本章定义的s_gets()函数,用指针表示法代替数组表示法便可减少一个变量i。请改写该函数

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;
}

10.strlen()函数接受一个指向字符串的指针作为参数,并返回该字符串的长度。请编写一个这样的函数

int sl(char* st)
{
    int n = 0;
    while(*st++)
    {
        n++;
    }
    return n;
}

11.本章定义的s_gets()函数,可以用strchr()函数代替其中的while循环来查找换行符。请改写该函数

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;
}

12.设计一个函数,接受一个指向字符串的指针,返回指向该字符串第1个空格字符的指针,或如果未找到空格字符,则返回空指针

char* fs(char* st)
{
    while(*st++)
    {
        if(*st == ' ')
        {
            return st;
        }
    }
    return NULL;
}

13.重写程序清单11.21,使用ctype.h头文件中的函数,以便无论用户选择大写还是小写,该程序都能正确识别答案

#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;
}

编程练习

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

#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;
}

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

#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;
}

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

#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;
}

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

#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;
}

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

#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;
}

6.编写一个名为is_within()的函数,接受一个字符和一个指向字符串的指针作为两个函数形参。如果指定字符在字符串中,该函数返回一个非零值(即为真)。否则,返回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;
}

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

#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;
}

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

#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
 */

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

#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;
}

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

#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

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

#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;
}

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

#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;
}

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

#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;
}

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

#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;
}

15.使用字符分类函数实现atoi()函数。如果输入的字符串不是纯数字,该函数返回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;
}

16.编写一个程序读取输入,直至读到文件结尾,然后把字符串打印出来。该程序识别和实现下面的命令行参数:

-p  按原样打印

-u  把输入全部转换成大写

-l  把输入全部转换成小写

如果没有命令行参数,则让程序像是使用了-p参数那样运行

#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;
}

 

10-03 19:50