//返回指针的函数
//输入字符串,并将其中的小写字母改为大写字母
char *upper(char *str)
{
char *dest = str;
while(*str != '\0')
{
if(*str >= 'a' && *str <= 'z')
{
*str -= 'a' - 'A';
}
str++;
}
return dest; //因为要返回输入字符串的首地址,所以用指针dest来保存,同样,函数类型也须是返回指针的函数
}
#include<stdio.h>
int main()
{
char c[20];
printf("Please input the strings\n");
gets(c); //gets可以读入包含空格、tab的字符串,直到遇到回车为止
//scanf("%s",c); 在这里不用scanf函数,因为scanf不能用来读取空格、tab
printf("Before upper, the strings are: \n %s\n",c);
char *dest = upper(c); //此时upper函数传递是实参c的地址,而非c的值
printf("After upper, the strings are: \n %s\n",dest);
return 0;
}
Input: I will be somebody.
Output: Before upper, the strings are: I will be some.body.
After upper, the strings are: I WILL BE SOMEBODY.