C语言 1.请输入一串字符并删除其中的空格,例如, 输入asd af aa z67, 则输出为 asdafaaz67 2.输入一个整数字符串转换为一个整数值 4.16

作业:

请输入一串字符并删除其中的空格,例如, 输入asd af aa z67, 则输出为 asdafaaz67

#include <stdio.h>
#include <string.h>
char *func(char *p,int len);
int main(int argc, char *argv[])
 
    char a[20];
    gets(a);
    func(a,20);
    printf("%s\n",a);
    return 0;
} 
char *func(char *p,int len)
{
    int i,j=0;
    for(i=0;i<len;i++)
    {
        if(p[i]!=' ')
        {
            p[j]=p[i];
            j++;
        }
    }
    p[j]='\0';
    return p;
}
hqyj@ubuntu:~/4.14$ gcc func.c
func.c: In function ‘main’:
func.c:13:5: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
   13 |     gets(a);
      |     ^~~~
      |     fgets
/usr/bin/ld: /tmp/cct6c0wP.o: in function `main':
func.c:(.text+0x2f): 警告: the `gets' function is dangerous and should not be used.
hqyj@ubuntu:~/4.14$ ./a.out
asd af aa z67
asdafaaz67

输入一个整数字符串转换为一个整数值,如”1234”转换为1234,”-1234”转换为-1234。编写转换函数chnum。

方法一: 利用Ascii码表之间的差值为48

#include <stdio.h>
#include <stdlib.h>
int chnum(char *p);
int main(int argc, char *argv[])
{ 
    char a[20];
    int i;
    gets(a);
    i=chnum(a);
    printf("%d\n",i);
    return 0;
} 
int chnum(char *p)
{
    int num=0;
    if(*p=='-')
    {
        p++;
        while(*p)
        {
            num=*p-48+num*10;
            p++;
        }
        return num=-num;
    }
    else
    {
        while(*p)
        {
            num=*p-48+num*10;
            p++;
        }
        return num;
    }
}

方法二:利用atoi的函数

 #include <stdlib.h>
   int main(int argc, char *argv[])
  {
      char a[5]="1234";
      int num=atoi(a);
      printf("%d\n",num);
      
      return 0;
  }

方法三:

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    char a[5];
    int n;
    gets(a);
    if(*a=='-')
    n=-(chnum(a+1));
    else
    n=chnum(a);
    printf("%d\n",n);
    return 0;
}
int chnum(char *p)
{
    int x=0,y,z,len;
    len=strlen(p);
    for(*p;*p!='\0';p++)
    {
        z=*p-'0';
        len--;
        for(y=len;y!=0;y--)
        {
            z=z*10;
        }
        x=x+z;
    }
    return x;
}

hqyj@ubuntu:~/4.14$ gcc test.c
test.c: In function ‘main’:
test.c:19:5: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
   19 |     gets(a);
      |     ^~~~
      |     fgets
/usr/bin/ld: /tmp/ccoUoziU.o: in function `main':
test.c:(.text+0x2f): 警告: the `gets' function is dangerous and should not be used.
hqyj@ubuntu:~/4.14$ ./a.out
-1234
-1234
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤独memories

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值