不返回字符串。该程序将 123 之类的数字转换为 "One Two Three" 之类的单词,为什么最后我没有得到任何东西?

Not returning string. This program converts digit like 123 to words like "One Two Three", why in the end I am not getting anything?

#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() 应该return 一个字符串,其中包含传递的单词中的数字等价物。它适用于单个数字(即 0-9 )但高于它就什么也没有。

函数有未定义的行为。

它returns一个指向局部数组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] = '[=10=]';

    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

请在发送最终字符串之前使用字符串复制。在你的代码中添加最后两行它会起作用。

    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
     }

1)

在你的函数str作为堆栈上的自动变量在int_to_string返回后被销毁。但是你需要 str 才能存活更多 int_to_string 个电话!所以你必须在调用之间保留 str

2)

case 0: return "Zero";
....

上面的代码在递归调用中将无法正常工作,单词"Zero"必须添加到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