#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LEN 81
char *s_gets(char *st, int n);
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 *);
void ToUpper(char * str)
{
while(*str)
{
*str = toupper(*str);
str++;
}
}
void ToLower(char * str)
{
while(*str)
{
*str = tolower(*str);
str++;
}
}
void Transpose(char * str)
{
while(*str)
{
if(islower(*str))
*str = toupper(*str);
else if (isupper(*str))
*str = tolower(*str);
str++;
}
}
void Dummy(char *str)
{
}
int main(void)
{
char line[LEN];
char copy[LEN];
char choice;
void(*pfun)(char *);
puts("please enter a string:\n");
while(s_gets(line, LEN) != 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 'd':pfun = Dummy;
break;
}
strcpy(copy, line);
show(pfun, copy);
}
puts("input a string(empty line to quit):\n");
}
puts("bye!\n");
return 0;
}
void show(void(*fp)(char *), char * str)
{
fp(str);
puts(str);
}
char showmenu(void)
{
char ans;
puts("enter menu choice:");
puts("(u) upper case (l)lowercase");
puts("(t) transpose (d)dummy ");
puts("(n) to next string");
ans = getchar();
ans = tolower(ans);
eatline();
while(strchr("ultdn", ans) == NULL)
{
puts("please enter a char between u l t d n");
ans = getchar();
ans = tolower(ans);
eatline();
}
return ans;
}
void eatline(void)
{
while(getchar() != '\n')
continue;
}
char *s_gets(char *st, int n)
{
char *find;
if(fgets(st, n, stdin))
{
find = strchr(st, '\n');
if(find)
*find = '\0';
else
eatline();
}
return st;
}