What is wrong with the number 123


  • 0
    H
    char* getword(int num,int mod)
    

    {
    if (mod==0)
    switch (num)
    {
    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;
    default:return "";break;
    }
    if (mod==1)
    switch (num)
    {
    case 10:return "Ten";break;
    case 11:return "Eleven";break;
    case 12:return "Twelve";break;
    case 13:return "Thirteen";break;
    case 14:return "Fourteen";break;
    case 15:return "Fifteen";break;
    case 16:return "Sixteen";break;
    case 17:return "Seventeen";break;
    case 18:return "Eighteen";break;
    case 19:return "Nineteen";break;
    case 2:return "Twenty";break;
    case 3:return "Thirty";break;
    case 4:return "Forty";break;
    case 5:return "Fifty";break;
    case 6:return "Sixty";break;
    case 7:return "Seventy";break;
    case 8:return "Eighty";break;
    case 9:return "Ninety";break;
    default:return "";break;
    }
    }

    char* numberToWords(int num)
    {

    int n=num;
    int x[4];
    char temp[500]="";
    

    /* if (num==123)
    {
    strcat(temp,"One Hundred Twenty Three");
    return temp;
    }
    */
    for (int i=3;i>=0;i--)
    {
    x[i]=n%1000;
    n/=1000;
    }

    for (int i=0;i<4;i++)
    {
    	if (x[i]>0)
    	{
    		int a=x[i]/100;
    		int b=(x[i]%100)/10;
    		int c=x[i]%10;
    		if (a>0)
    		{
    			strcat(temp,getword(a,0));
    			strcat(temp," Hundred ");
    		}
    		if (b==1)
    			strcat(temp,getword(10+c,1));
    		else
    		{
    			strcat(temp,getword(b,1));
    			if (c>0)
    			{
    				if ((!(temp[strlen(temp)-1]==' '))&&strlen(temp)>0) strcat(temp," ");
    				strcat(temp,getword(c,0));
    			}
    		}
    		switch (i)
    		{
    			case 0:strcat(temp," Billion ");break;
    			case 1:strcat(temp," Million ");break;
    			case 2:strcat(temp," Thousand ");break;
    			default:break;
    		}
    	}
    }
    printf("%d\n",strlen(temp));
    printf("%s\n",temp);
    if (temp[strlen(temp)-1]==' ') temp[strlen(temp)-1]='\0';
    return temp;
    

    }

    Everthing works well on my computer.

    However when running on Leetcode, the number 123 always get the answer of "One Hundred Twen
    Yes, the last " is also missing.
    And when I use printf to debug, the stdout for 123 is correct.
    Is there anything I miss or it's just a bug of Leetcode?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.