1、原始函数,使用下标运算符
#include <stdio.h> #include <ctype.h> void upper(char x[]) { int tmp = 0; while(x[tmp]) { x[tmp] = toupper(x[tmp]); tmp++; } } void lower(char x[]) { int tmp = 0; while(x[tmp]) { x[tmp] = tolower(x[tmp]); tmp++; } } int main(void) { char str1[128] = "ABcd"; upper(str1); printf("upper result: %s\n", str1); lower(str1); printf("lower result: %s\n", str1); return 0; }
2、使用指针
#include <stdio.h> #include <ctype.h> void upper(char *s1) { while(*s1) { *s1 = toupper(*s1); s1++; } } void lower(char *s1) { while(*s1) { *s1 = tolower(*s1); s1++; } } int main(void) { char str1[128] = "ABcd"; upper(str1); printf("upper result: %s\n", str1); lower(str1); printf("lower result: %s\n", str1); return 0; }