#include<stdio.h>
#include<string.h>
char *int_to_string( int n );
void main()
{
int n;
printf("Enter the number : ");
scanf("%d",&n);
printf("\n%d in words is : %s.\n",n,int_to_string(n));
}
char *int_to_string( int n)
{
char str[100]="";
if(n<10)
{
switch(n)
{
case 0: return "Zero";
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
}
}
else
{
strcat(str,int_to_string(n/10));
strcat(str," ");
return strcat(str,int_to_string(n%10));
}
}
函数int_to_string()应该返回一个字符串,其中包含与传递的单词中的数字相等的值。它适用于单个数字(即0-9),但高于它只是什么都没有。
答案 0 :(得分:3)
该函数具有未定义的行为。
它返回一个指向本地数组str
的指针,该指针在退出函数后通常会被销毁。
考虑到最好将参数定义为类型unsigned int
。否则该函数需要检查该数字是否为负数。
您可以通过声明第二个参数来简化任务,该参数将指定将存储结果字符串的字符零终止数组。
或者你必须动态分配内存。
以下是两种方法。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char * int_to_string( unsigned int n )
{
if( n < 10 )
{
char *p = "";
switch( n )
{
case 0:
p = "Zero";
break;
case 1:
p = "One";
break;
case 2:
p = "Two";
break;
case 3:
p = "Three";
break;
case 4:
p = "Four";
break;
case 5:
p = "Five";
break;
case 6:
p = "Six";
break;
case 7:
p = "Seven";
break;
case 8:
p = "Eight";
break;
case 9:
p = "Nine";
break;
}
char *q = malloc( strlen( p ) + 1 );
strcpy( q, p );
free( p );
return q;
}
else
{
char *q = int_to_string( n / 10 );
char *p = int_to_string( n % 10 );
q = realloc( q, strlen( q ) + strlen( p ) + 2 );
strcat( q, " " );
return strcat( q, p );
}
}
char * int_to_string1( unsigned int n, char *s )
{
if( n < 10 )
{
char *p = "";
switch( n )
{
case 0:
p = "Zero";
break;
case 1:
p = "One";
break;
case 2:
p = "Two";
break;
case 3:
p = "Three";
break;
case 4:
p = "Four";
break;
case 5:
p = "Five";
break;
case 6:
p = "Six";
break;
case 7:
p = "Seven";
break;
case 8:
p = "Eight";
break;
case 9:
p = "Nine";
break;
}
return strcat( s, p );
}
else
{
strcat( int_to_string1( n / 10, s ), " " );
return int_to_string1( n % 10, s );
}
}
int main( void )
{
unsigned int n = 1234567890;
char *s = int_to_string( n );
puts( s );
free( s );
char s1[100];
s1[0] = '\0';
puts( int_to_string1( n, s1 ) );
}
程序输出
One Two Three Four Five Six Seven Eight Nine Zero
One Two Three Four Five Six Seven Eight Nine Zero
答案 1 :(得分:2)
请在发送最终字符串之前使用字符串副本。在代码中添加最后两行,它将起作用。
char *int_to_string( int n)
{
char str[100]="";
char str1[100]="";//
.
.
.
strcat(str,int_to_string(n/10));
strcat(str," ");
strcat(str,int_to_string(n%10));
strcpy(str1,str);//create one more str1 array of 100 and copy final data
return str1;// return str1 array of data
}
答案 2 :(得分:1)
1)
在函数str
中,在int_to_string
返回后,堆栈上的自动变量被销毁。但是,您需要str
才能有更多int_to_string
次来电!因此,您必须在通话之间保留str
。
2)
case 0: return "Zero";
....
上面的代码在递归调用中无效,单词&#34; Zero&#34;必须添加到str
字符串
case 0: strcat(str,"Zero"); return str;
但是为什么要使用递归调用呢?可以用简单的循环替换递归。显示了两种解决方案。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
void print_digit(int digit)
{
switch(digit)
{
case '0': printf("Zero "); break;
case '1': printf("One "); break;
case '2': printf("Two "); break;
case '3': printf("Three ");break;
case '4': printf("Four "); break;
case '5': printf("Five "); break;
case '6': printf("Six "); break;
case '7': printf("Seven ");break;
case '8': printf("Eight ");break;
case '9': printf("Nine "); break;
}
}
char * return_digit_word(int digit)
{
switch(digit)
{
case '0': return("Zero "); break;
case '1': return("One "); break;
case '2': return("Two "); break;
case '3': return("Three ");break;
case '4': return("Four "); break;
case '5': return("Five "); break;
case '6': return("Six "); break;
case '7': return("Seven ");break;
case '8': return("Eight ");break;
case '9': return("Nine "); break;
}
}
char *int_to_string(int n,char str[],char numStr[])
{
if(n<10)
{
switch(n)
{
case 0: strcat(str,"Zero");break;
case 1: strcat(str,"One");break;
case 2: strcat(str,"Two");break;
case 3: strcat(str,"Three");break;
case 4: strcat(str,"Four");break;
case 5: strcat(str,"Five");break;
case 6: strcat(str,"Six");break;
case 7: strcat(str,"Seven");break;
case 8: strcat(str,"Eight");break;
case 9: strcat(str,"Nine");break;
}
return str;
}
else{
int digit = numStr[0]-'0';
int newNr = n - digit*pow(10,strlen(numStr)-1);
strcat(str, return_digit_word(numStr[0]));
sprintf(numStr, "%d", newNr);
return int_to_string(newNr,str,numStr);
}
}
int main(void) {
int n,i;
char str[100]="";
char numStr[100]="";
n = 1234567890;
sprintf(numStr, "%d", n);
printf("\n%d in words is : %s\n",n, int_to_string(n,str,numStr) );
printf("\n%d in words is : ",n);
sprintf(numStr, "%d", n);
for(i=0;i<strlen(numStr);i++)
{
print_digit(numStr[i]);
}
return 0;
}
n = 1234567890的输出:
1234567890 in words is : One Two Three Four Five Six Seven Eight Nine Zero
1234567890 in words is : One Two Three Four Five Six Seven Eight Nine Zero