Thursday, September 17, 2009

challenge of GetInt(char* str, int num)

void GetInt(char *str, int num){

int n=num;

int count=0;

while(n>0){

n/=10;

count++;

}

*(str+count)='\0';

for(;count>0;count--){

*(str+count-1)=num%10+'0';

num/=10;

}

}

Here is all my code for this challenge. It's different from the notes in class because we cannot use std library, so the first thing came to my mind was use a number plus '0', but at the very first, I need to know how long this number is. And I write while(n>0){ n/=10; count++; } . Then, I change the integer to string from very last to the beginning, so I use the for loop for(;count>0;count--){ *(str+count-1)=num%10+'0'; num/=10; } . Finally, I just want to end the string, and I got *(str+count)='\0'; in the middle. That's all my steps.

No comments:

Post a Comment