【C編程練習】2013華爲校園招聘機試題9月10日題1:把整數轉換成字符串 void ConvertIntToStr(int nVal, char* pStr);

【C編程練習】2013華爲校園招聘機試題9月10日題1:把整數轉換成字符串 void ConvertIntToStr(int nVal, char* pStr);

//ex7_function4.c

//編寫於2012年9月11日,源作者:曹玲玲

//【實現功能】2013華爲校園招聘機試題9月10日題1:

/*【題目】第一道題,把整數轉換成字符串 void ConvertIntToStr(int nVal, char* pStr); 

*/

#include <stdio.h>

#define MAX 100

void ConvertIntToStr(int nVal, char* pStr)

{

  int inter,remainder,i=0,j=0;

  char tempStr[MAX];

  inter=nVal/10;

  remainder=nVal%10;

  while(inter)

  {

    tempStr[i]=remainder+48;

    i++;

    remainder=inter%10;

    inter=inter/10;

 

  }

  tempStr[i]=remainder+48;

  for(;i>=0;i--,j++)

  {

    pStr[j]=tempStr[i];

  }

  pStr[j]='\0';

}

void ConvertIntToStr_main()

{

  int number,temp1;

  char pStr[MAX];

  printf("請輸入一個整數:");

  temp1=scanf("%d",&number);

  while (temp1==1)

  {

    ConvertIntToStr(number, pStr);

    printf("%d轉換成字符串爲:%s\n",number,pStr);

 

    printf("\n\n請輸入下一整數。輸入其他字符結束輸入!\n");

    temp1=scanf("%d",&number);

  }

}

運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章