yexuannan

1、整型转化为字符串

整数+\'0’隐性转化为char类型的数

void main()
{
    int aa=1;
    char t=1+\'0\';
    printf("%d\n",t);
    printf("%c\n",t);
}

image

#include<stdio.h>

void main()
{
    int n=12345;
    int i=0;
    int j=0;
    char temp[6];
    char temp1[6];
    while(n!=0)
    {
        temp[i]=n%10+\'0\';
        n=n/10;
        i++;
    }
    //temp=\'0\';这句话是错的,
    temp[i]=NULL;//temp=\'\0\'
    i--;
    while(i>=0)
    {
        temp1[j]=temp[i];
        j++;
        i--;
    }
    temp1[j]=NULL;
    printf("%s\n",temp1);
}

2、字符串转化为整数

#include<stdio.h>
#include<string.h>
void main()
{
    char string[7]="123456";
    int len=strlen(string);
    int i=0;
    int sum=0;
    while(i<len)
    {
        sum=sum*10+(string[i]-\'0\');
        i++;
    }
    printf("%d",sum);
}

3、字符串拷贝函数

#include<stdio.h>
#include<malloc.h>
char *strcpy1(char *strDest, const char *strSrc)
{
    if(strSrc==NULL||strDest==NULL)
        printf("拷贝失败\n");
   //assert(strSrc!=NULL&&strDest!=NULL);        
    char *temp=strDest;
    while((*strSrc)!=\'\0\')
    {
        *temp=*strSrc;
        temp++;
        strSrc++;
    }
    *temp=\'\0\';
    return strDest;
}

void main()
{
    char *s="1234";
    char *ss=(char*)malloc(sizeof(char *));
    strcpy1(ss,s);
    printf("%s",ss);
}

分类:

技术点:

相关文章:

  • 2021-11-05
  • 2021-12-20
  • 2021-12-10
  • 2021-11-17
  • 2021-12-27
  • 2021-11-07
  • 2021-04-30
  • 2021-06-09
猜你喜欢
  • 2021-12-12
  • 2021-09-07
  • 2021-10-19
  • 2021-10-19
  • 2021-11-03
  • 2021-10-19
  • 2021-10-19
相关资源
相似解决方案