The function that returns the pointer (C Programming)


//返回指针的函数
//输入字符串,并将其中的小写字母改为大写字母
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.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值