array help! please

Reply

Join Date: Nov 2009
Posts: 10
Reputation: lisedaton is an unknown quantity at this point 
Solved Threads: 0
lisedaton lisedaton is offline Offline
Newbie Poster

array help! please

 
0
  #1
Dec 10th, 2009
hi everybody!...
so i have this assigment for monday and im having trouble with it... the program in c should get:

input:
a sentence.....nanny do you have any cheap peach...

and it should print the "same" words...:
nanny any
cheap peach

..they are called "same words when they get the same letter no matter upper or lower case and how many time each letter....

so this is my code i have no idea whats wrong... but can somebody help me fixe it????


thank you alll!


  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char wordA(char sentence[80], int x);
  5. char wordB(char sentence[80], int x);
  6. int endwordA(char sentence[80], int x);
  7. int endwordB(char sentence[80], int x);
  8. int checkword(char word1[80]);
  9.  
  10. int main()
  11. {
  12. char sentence[80], word1[80], word2[80];
  13. int x=0, endword1=0, endword2=0, counter1, counter2, endwordimp;
  14.  
  15. // receives the input from the user
  16. printf("Please enter a sentence\n");
  17. gets(sentence);
  18.  
  19. //running the sentence
  20. while (sentence[x]!='\0')
  21. {
  22. int i=0;
  23. int w=1;
  24. //separating the first word.....
  25. word1[x]= wordA(sentence, x);
  26. endword1= endwordA(sentence, x);
  27. endwordimp = endword1;
  28. while (i!=w)
  29. {
  30. // searching for the second word....
  31. word2[x]= wordB(sentence, endword1);
  32. endword2= endwordB(sentence, endword1);
  33.  
  34. //cheking the letters of each word....//....returns the real value of each letter in the word!...
  35. counter1= checkword(word1);
  36. counter2= checkword(word2);
  37.  
  38. // comparing the words...
  39. if (counter1==counter2)
  40. {
  41. printf ("%s %s", word1, word2);
  42. endword1= endword2;
  43. if (sentence[endword2]=='\0' || sentence[endword2+1]=='\0')
  44. {
  45. i=w;
  46. sentence[x]= sentence[endwordimp];
  47. }
  48. }
  49. else
  50. {
  51. endword1= endword2;
  52. if (sentence[endword2]=='\0' || sentence[endword2+1]=='\0')
  53. {
  54. i=w;
  55. sentence[x]= sentence[endwordimp];
  56. }
  57. }
  58. }
  59. }
  60.  
  61. system("PAUSE");
  62. return 0;
  63. }
  64.  
  65.  
  66. // FUNCTION THAT CHECKS THE WORDS
  67. int checkword(char word1[80])
  68. {
  69. int counter=0, x=0;
  70. for (x; word1!='\0'; x++)
  71. {
  72. if ((word1[x]>=65) && (word1[x]<=90))
  73. {
  74. counter= (int)word1[x];
  75. }
  76. else
  77. {
  78. counter= (int)word1[x] - 32;
  79. }
  80. }
  81. return (counter);
  82. }
  83.  
  84.  
  85.  
  86.  
  87. ///// FUNCTION THAT CREATE ME THE FIRST WORD
  88. char wordA(char sentence[80], int x)
  89. {
  90. char word1[80];
  91. int y=0;
  92. for (x; x<=strlen(sentence) ; x++)
  93. {
  94. while ((sentence[x]>122 || sentence[x]<97) && (sentence[x]>90 || sentence[x]<65))
  95. {
  96. x++;
  97. }
  98. for (x; x<=strlen(sentence); x++)
  99. {
  100. if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
  101. {
  102. word1[y]=sentence[x];
  103. y++;
  104. }
  105. else
  106. {
  107. word1[y]='\0';
  108. x++;
  109. break;
  110. }
  111. break;
  112. }
  113. break;
  114. }
  115. return (word1);
  116. }
  117.  
  118. /////////////////// functions that takes me to the end of the first word!
  119. int endwordA(char sentence[80], int x)
  120. {
  121. char word1[80];
  122. int y=0;
  123. for (x; x<=strlen(sentence) ; x++)
  124. {
  125. if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
  126. {
  127. word1[y]=sentence[x];
  128. y++;
  129. }
  130. else
  131. {
  132. word1[y]='\0';
  133. x++;
  134. break;
  135. }
  136. }
  137. return (x);
  138. }
  139.  
  140. /// FUNCTINOS THAT GIVES ME THE SECOND WORD... (WORD TO COMPARE)
  141. char wordB(char sentence[80], int endword1)
  142. {
  143. int y=0;
  144. char word2[80];
  145. int x=endword1;
  146. for (x; x<=strlen(sentence) ; x++)
  147. {
  148. while ((sentence[x]>122 || sentence[x]<97) && (sentence[x]>90 || sentence[x]<65))
  149. {
  150. x++;
  151. }
  152. for (x; x<=strlen(sentence); x++)
  153. {
  154. if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
  155. {
  156. word2[y]=sentence[x];
  157. y++;
  158. }
  159. else
  160. {
  161. word2[y]='\0';
  162. x++;
  163. break;
  164. }
  165. }
  166. break;
  167. }
  168. return (word2[80]);
  169. }
  170.  
  171. /////// FUNCTION THAT TAKES ME TO THE END OF THE SECOND WORD.......
  172. int endwordB(char sentence[80], int endword1)
  173. {
  174. char word1[80];
  175. int y=0;
  176. int x=endword1;
  177. for (x; x<=strlen(sentence) ; x++)
  178. {
  179. if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
  180. {
  181. word1[y]=sentence[x];
  182. y++;
  183. }
  184. else
  185. {
  186. word1[y]='\0';
  187. x++;
  188. break;
  189. }
  190. }
  191. return (x);
  192. }

thanx!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 662
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 112
Murtan Murtan is offline Offline
Practically a Master Poster
 
0
  #2
Dec 11th, 2009
When asking for help, please describe the symptoms you do see (if any).

For debugging purposes, you should add some print statements to your code to confirm that you are getting the data you expect, where you expect it and for progress messages so you can tell where you stopped at if your program doesn't complete.

If this was c++ code it wouldn't compile (the return value on line 115 does not match the specified return type on line 88)

Your wordA and endWordA functions do not quite use the same algorithm.

The for statement on line 70 in checkword will likely cause an infinite loop. The test condition should be for word1[x] even if you fixed that, the function does not do what you need it to do. It needs to form a representation of the letters that are in the word, regardless of order or frequency. The algorithm you have works for order, but not for frequency.

If you're not attached to them, changing the prototype for wordA might allow you to actually do what you're attempting and return where the word ended at the same time.

  1. int nextWord(char sentence[80], int startx, char wordfound[80]);

Where it will return where the word ended and fill the wordfound array with the word it finds.


A 'better' overall algorithm might be:
  • force the sentence to upper or lower case
    I don't see where you have to preserve the case of the input
  • split the sentence into words, keeping all of the words
    you might look at strtok(), strspn() and strcspn() for help splitting
    you will probably need an array of words
  • "score" the words
    you might consider a bit-based algorithm
  • now compare the scores of all the words

The above has the advantage of having intermediate 'deliverables' that you could output and verify to confirm that things are working as you expect them to.

If you don't want to change algorithms mid-stream, I understand, I'll still try to help you make what you're trying to do work. But you've still got a lot of work to go on what you've started.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 304 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC