Idea

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Idea

 
0
  #11
Jun 26th, 2009
I used a single string array for ease! Merely used a delta index for the extra data.

index 1's
index + 18 10's
index + 29 100's
index + 30 1000's
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Idea

 
0
  #12
Jun 26th, 2009
till now i made this
  1. char *IntToarray(int num)
  2. {
  3. char *pch;
  4. int j;
  5. char temp[100];
  6. if(num>0 || num<=19)
  7. pch=string[num];
  8. if(num>=20 || num<=100) {
  9. if(num>=20 || num <=29) {
  10. j=num %10;
  11. strcpy(temp,string[20]);
  12. strcat(temp," ");
  13. strcat(temp,string[j]);
  14. puts(temp);
  15. }
  16. }
  17. }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Idea

 
0
  #13
Jun 26th, 2009
i didnt read the other posts after i posted just now but i think my codes are just sloppy now i was thinking of like searching 20 30 40 50 60 but that will take alot of functions maybe i should write that into a function
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Idea

 
0
  #14
Jun 26th, 2009
You have to use a scratch buffer to build a concatenated string. In your implementation you were contaminating the original source nouns by concatenating to them!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Idea

 
0
  #15
Jun 26th, 2009
Using your latest post, it still has the math error.
Still using your two arrays!


  1. char *IntToarray(int num)
  2. {
  3. char *pch;
  4. int j;
  5. char temp[100];
  6. if(num>0 || num<=19)
  7. pch=string1[num];
  8. else if(num>=20 || num<=100) {
  9.  
  10. j=num %10;
  11. num = num / 10;
  12.  
  13. strcpy(temp,string2[num]);
  14. if (j != 0)
  15. {
  16. strcat(temp,"-");
  17. strcat(temp,string1[j]);
  18. }
  19.  
  20. puts(temp);
  21. }
  22. }

Using the single array


  1. char *IntToarray(int num)
  2. {
  3. char *pch;
  4. int j;
  5. char temp[100];
  6. if(num>0 || num<=19)
  7. pch=string[num];
  8. else if(num>=20 || num<=100) {
  9.  
  10. j=num %10;
  11. num = num / 10;
  12.  
  13. strcpy(temp,string[num + 18]);
  14. if (j != 0)
  15. {
  16. strcat(temp,"-");
  17. strcat(temp,string[j]);
  18. }
  19.  
  20. puts(temp);
  21. }
  22. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Idea

 
0
  #16
Jun 26th, 2009
I've got to run but does this make sense now?
I'd recommend using one single buffer, and printing it when the entire string is built!

Also note, the cases of { 20, 100 } can be handled wither way as the result is the same index!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Idea

 
0
  #17
Jun 26th, 2009
So with a little cleanup...

  1. char *IntToarray( char *szBuf, int num)
  2. {
  3. int j;
  4.  
  5. if(num>0 || num<=19)
  6. strcpy( szBuf, string[num] );
  7. else if(num>=20 || num<=100) {
  8.  
  9. j=num %10;
  10. num = num / 10;
  11.  
  12. strcpy( szBuf, string[num + 18]);
  13. if (j != 0)
  14. {
  15. strcat( szBuf, "-");
  16. strcat( szBuf, string[j]);
  17. }
  18. }
  19.  
  20. return szBuf; // return the passed in buffer!
  21. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Idea

 
0
  #18
Jun 26th, 2009
Missed a typo in your original. Zero was being ignored!
Also missed your || instead of &&
And only need to check for the upper range after the first test!

  1. char *IntToarray( char *szBuf, int num)
  2. {
  3. int j;
  4.  
  5. *szBuf = 0; // Preset for error case!
  6.  
  7. if (num>=0 && num<=19)
  8. strcpy( szBuf, string[num] );
  9. else if( num<=100) {
  10. j=num %10;
  11. num = num / 10;
  12.  
  13. strcpy( szBuf, string[num + 18]);
  14.  
  15. if (j != 0)
  16. {
  17. strcat( szBuf, "-");
  18. strcat( szBuf, string[j]);
  19. }
  20. }
  21.  
  22. return szBuf; // return the passed in buffer!
  23. }
Last edited by wildgoose; Jun 26th, 2009 at 10:06 pm. Reason: code
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Idea

 
0
  #19
Jun 26th, 2009
okay i almost done it for but when i return it to char * it messed it up
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define DEBUG
  4. char *string[]= {
  5. "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
  6. "sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","hundreed"
  7. };
  8. //from int to array and return char string
  9.  
  10. /* n will be the index of the array like if its twenty thirty fourty etc*/
  11. /*n2 will be the reminder*/
  12. void ParseString(char *buffer1,char **buffer2,int n,int n2)
  13. {
  14. strcpy(buffer1,buffer2[n]);
  15. strcat(buffer1," ");
  16. strcat(buffer1,buffer2[n2]);
  17. }
  18. char *IntToarray(int num)
  19. {
  20. char *pch;
  21. int j;
  22. int z;
  23. char temp[100];
  24. if(num>0 || num<=19)
  25. pch=string[num];
  26. if(num==20 || num <100) {
  27. j=num % 10;
  28. num=( num /10) +18;
  29. if(j==0)
  30. return pch=string[num];
  31. ParseString(temp,string,num,j);
  32. //puts(temp); this work i dunno why it get messed
  33. return pch=temp;//this doesnt work
  34. }
  35. }
  36. int main(void)
  37. {
  38. char *pch;
  39. pch=IntToarray(21);
  40. puts(pch);
  41. getchar();
  42. return 0;
  43. }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Idea

 
0
  #20
Jun 26th, 2009
i did till now but i got problem now now it when i get to like 111 or more i will get reminder 1 etc so it wont be from one hundered eleven it will be one hundered 1
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define DEBUG
  4. char *string[]= {
  5. "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
  6. "sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","one hundreed",
  7. "two hundered","three hundered"
  8. };
  9. //from int to array and return char string
  10.  
  11. /* n will be the index of the array like if its twenty thirty fourty etc*/
  12. /*n2 will be the reminder*/
  13. #define LESSTHAN100 0
  14. #define MORETHAN100 1
  15. void ParseString(char *buffer1,char **buffer2,int n,int n2,int choice)
  16. {
  17. if(!choice) {
  18. strcpy(buffer1,buffer2[n]);
  19. strcat(buffer1," ");
  20. strcat(buffer1,buffer2[n2]);
  21. }
  22. else {
  23. strcat(buffer1,buffer2[n]);
  24. strcat(buffer1," ");
  25. strcat(buffer1,"and");
  26. strcat(buffer1," ");
  27. strcat(buffer1,buffer2[n2]);
  28. }
  29. }
  30. char *IntToarray(int num)
  31. {
  32. char *pch;
  33. int j;
  34. int z;
  35. char temp[100];
  36. if(num==0 || num<=19){
  37. puts("we returned");
  38. return pch=string[num];
  39. }
  40. else if(num==20 || num <100) {
  41. j=num % 10;
  42. num=( num /10) +18;
  43. if(j==0)
  44. return pch=string[num];
  45. ParseString(temp,string,num,j,LESSTHAN100);
  46. puts(temp);
  47. }
  48. else if(num==100 || num<=999) {
  49. j=num % 10;
  50. num= (num/10) +18;
  51. if(j==0)
  52. return pch=string[num];
  53. ParseString(temp,string,num,j,MORETHAN100);
  54. puts(temp);
  55. }
  56. }
  57. int main(void)
  58. {
  59. char *pch;
  60. pch=IntToarray(41);
  61. //puts(pch);
  62. getchar();
  63. return 0;
  64. }
anyways i will go sleep now i hope tommrow i will fix those errors.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1342 | Replies: 36
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC