天天看點

C語言函數指針練習

#include <stdio.h>

#include <string.h>

#include <ctype.h>

char showmenu(void);

void eatline(void);

void show(void (* fp)(char *),char * str);

void toupper(char *);

void tolower(char *);

void transpose(char *);

void dummy(char *);

int main(void){

    char line[81];

    char copy[81];

    char choice;

    void (*pfun)(char *);

    puts("enter a string(empty line to quit):");

    while(gets(line)!=null&&line[0]!='\0'){

        while((choice=showmenu())!='\n'){

            switch(choice){

                case 'u':pfun=toupper;break;

                case 'l':pfun=tolower;break;

                case 't':pfun=transpose;break;

                case 'o':pfun=dummy;break;

            }

            strcpy(copy,line);

            show(pfun,copy);

        }

        puts("enter a string (empty line to quit)");

    }

    puts("bye!\n");

    return 0;

}

char showmenu(void){

    char ans;

    puts("enter menu choice:");

    puts("u) uppercase l) lowercase");

    puts("t) transposed case 0) original case");

    puts("n) next string");

    ans=getchar();

    ans=tolower(ans);

    eatline();

    while(strchr("ulton",ans)==null){

        puts("please enter a u,l,t,o or n:");

        ans=tolower(getchar());

        eatline();

    return ans;

void eatline(void){

    while(getchar()!='\n'){

        continue;

void toupper(char * str){

    while(*str){

        *str=toupper(*str);

        str++;

void tolower(char * str){

        *str=tolower(*str);

void transpose(char * str){

    if(islower(*str)){

    }else if(isupper(*str)){

    str++;

void dummy(char * str){

void show(void(* fp)(char *),char * str){

    (*fp)(str);

    puts(str);

繼續閱讀