944,174 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4470
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 29th, 2005
0

Scanning an inputted array of words, and separating each word into a new array?

Expand Post »
Is this possible? I would like to have a user input a sentence, and then have each word in that sentence be put into its own array.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Loopah is offline Offline
60 posts
since Aug 2005
Nov 29th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

Yes it is possible.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

make use of string class and its functions...it is very easy
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

Oh, thanks for the information. I will be sure to look at the string class tonight when I get a chance to finish my program. I assume you are talking about the <string.h> class right? If so, I have tried using the strtok function to break each word into a function. After trying this I cannot seem to figure out how to separate each token into a new array. Or is that even needed to do what I need to do? Do I need to use something other than strtok?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Loopah is offline Offline
60 posts
since Aug 2005
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

I was talking about <string>
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

Oh, I have never heard of that. I am using C, not C++ if that makes a difference. Do you know about the strtok function that I am speaking of?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Loopah is offline Offline
60 posts
since Aug 2005
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

Post your attempt. The basics are something like this:
  1. static const char delim[] = " \n";
  2. size_t size = 0;
  3. char word[10][20], *ptr = strtok(sentence, delim);
  4. while ( ptr != NULL )
  5. {
  6. strcpy(word[size], ptr);
  7. if ( ++size >= sizeof word / sizeof *word )
  8. {
  9. break;
  10. }
  11. ptr = strtok(NULL, delim);
  12. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

Ok, this is my program. I included the entire thing, because when I went to add the part in the main() function about choosing which game to play, it gives an error when I run it. Something is wrong and I have no clue what it is. My attempt at the arrays is in the cellPhone function which is not complete.

Also my compiler is giving me an error if i try to shorten the line length of the menu by entering half of it to a new line. Why is it doing this?

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void nameGame();
  5. void pigLatin();
  6. void changeLatin(char [], int);
  7. void cellPhone();
  8. void cellButtons(char [], int);
  9.  
  10. main()
  11. {
  12. int input = 0;
  13.  
  14. printf("\n\tEnter your choice:\n\t1. Name Game\n\t2. Pig Latin\n\t3. Text Messaging\n\t4. Exit\n");
  15.  
  16. scanf("%d", &input);
  17.  
  18. while (input != 4)
  19. {
  20. if (input == 1)
  21. nameGame();
  22. else if (input == 2)
  23. pigLatin();
  24. else if (input == 3)
  25. cellPhone();
  26. else
  27. printf("Invalid choice. Enter a number 1-4.");
  28.  
  29. printf("\n\tEnter your choice:\n\t1. Name Game\n\t2. Pig Latin\n\t3. Text Messaging\n\t4. Exit\n");
  30. scanf("%d", &input);
  31. }
  32.  
  33. return 0;
  34.  
  35. }
  36.  
  37. void nameGame()
  38. {
  39. char c, *stringVowel, firstName[25];
  40. const char *stringVowels = "aeiou";
  41. int i = 0;
  42. puts("Enter a first name: ");
  43.  
  44. while ( ( c = getchar() ) != '\n')
  45. firstName [i++] = c;
  46.  
  47. firstName[i] = '\0';
  48.  
  49. stringVowel = strpbrk(firstName, stringVowels);
  50.  
  51. if (firstName[0] == 'B' || firstName[0] == 'b')
  52. {
  53. printf("\n%s!\n", firstName);
  54. printf("%s, %s bo %s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel),
  55. strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
  56. }
  57.  
  58. else if (firstName[0] == 'F' || firstName[0] == 'f')
  59. {
  60. printf("\n%s!\n", firstName);
  61. printf("%s, %s bo B%s Bonana fanna fo %s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel),
  62. strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
  63. }
  64.  
  65. else if (firstName[0] == 'M' || firstName[0] == 'm')
  66. {
  67. printf("\n%s!\n", firstName);
  68. printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo %s, %s!", firstName, firstName, strstr(firstName, stringVowel),
  69. strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
  70. }
  71.  
  72. else if (firstName[0] == 'A' || firstName[0] == 'a')
  73. {
  74. firstName[0] = 'a';
  75. printf("\n%s!\n", firstName);
  76. printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
  77. firstName, firstName, firstName);
  78. }
  79.  
  80. else if (firstName[0] == 'E' || firstName[0] == 'e')
  81. {
  82. firstName[0] = 'e';
  83. printf("\n%s!\n", firstName);
  84. printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
  85. firstName, firstName, firstName);
  86. }
  87.  
  88. else if (firstName[0] == 'I' || firstName[0] == 'i')
  89. {
  90. firstName[0] = 'i';
  91. printf("\n%s!\n", firstName);
  92. printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
  93. firstName, firstName, firstName);
  94. }
  95.  
  96. else if (firstName[0] == 'O' || firstName[0] == 'o')
  97. {
  98. firstName[0] = 'o';
  99. printf("\n%s!\n", firstName);
  100. printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
  101. firstName, firstName, firstName);
  102. }
  103.  
  104. else if (firstName[0] == 'U' || firstName[0] == 'u')
  105. {
  106. firstName[0] = 'u';
  107. printf("\n%s!\n", firstName);
  108. printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
  109. firstName, firstName, firstName);
  110. }
  111.  
  112. else
  113. {
  114. printf("\n%s!\n", firstName);
  115. printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel),
  116. strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
  117. }
  118. }
  119.  
  120. void pigLatin()
  121. {
  122. char c, d, sentence[200], tempSentence[200], *wordPtr;
  123. int i = 0, j = 0;
  124. puts("Enter a sentence: ");
  125.  
  126. while ( ( c = getchar() ) != '\n')
  127. sentence [i++] = c;
  128.  
  129. sentence[i] = '\0';
  130.  
  131. wordPtr = strtok(sentence, " ");
  132.  
  133. i = 0;
  134.  
  135. while (wordPtr != NULL)
  136. {
  137. printf("%s\t", wordPtr);
  138.  
  139. while ( ( d = getchar() ) != '\0')
  140. tempSentence [i++] = d;
  141.  
  142. printf("%s\n", tempSentence);
  143.  
  144. wordPtr = strtok(NULL, " ");
  145. }
  146. }
  147.  
  148. void changeLatin(char sentence[200], int i)
  149. {
  150.  
  151. }
  152. void cellPhone()
  153. {
  154. char c, sentence[200];
  155. int i = 0, j = 0;
  156. puts("Enter a sentence: ");
  157.  
  158. while ( ( c = getchar() ) != '\n')
  159. sentence [i++] = c;
  160.  
  161. sentence[i] = '\0';
  162.  
  163. cellButtons(sentence, i);
  164. }
  165.  
  166. void cellButtons(char sentence[], int i)
  167. {
  168. int j = 0;
  169.  
  170. for(j = 0; j < i; j++)
  171. {
  172. if (sentence[j] == 'A')
  173. printf("*2");
  174. else if (sentence[j] == 'B')
  175. printf("*22");
  176. else if (sentence[j] == 'C')
  177. printf("*222");
  178. else if (sentence[j] == 'D')
  179. printf("*3");
  180. else if (sentence[j] == 'E')
  181. printf("*33");
  182. else if (sentence[j] == 'F')
  183. printf("*333");
  184. else if (sentence[j] == 'G')
  185. printf("*4");
  186. else if (sentence[j] == 'H')
  187. printf("*44");
  188. else if (sentence[j] == 'I')
  189. printf("*444");
  190. else if (sentence[j] == 'J')
  191. printf("*5");
  192. else if (sentence[j] == 'K')
  193. printf("*55");
  194. else if (sentence[j] == 'L')
  195. printf("*555");
  196. else if (sentence[j] == 'M')
  197. printf("*6");
  198. else if (sentence[j] == 'N')
  199. printf("*66");
  200. else if (sentence[j] == 'O')
  201. printf("*666");
  202. else if (sentence[j] == 'P')
  203. printf("*7");
  204. else if (sentence[j] == 'Q')
  205. printf("*77");
  206. else if (sentence[j] == 'R')
  207. printf("*777");
  208. else if (sentence[j] == 'S')
  209. printf("*7777");
  210. else if (sentence[j] == 'T')
  211. printf("*8");
  212. else if (sentence[j] == 'U')
  213. printf("*88");
  214. else if (sentence[j] == 'V')
  215. printf("*888");
  216. else if (sentence[j] == 'W')
  217. printf("*9");
  218. else if (sentence[j] == 'X')
  219. printf("*99");
  220. else if (sentence[j] == 'Y')
  221. printf("*999");
  222. else if (sentence[j] == 'Z')
  223. printf("*9999");
  224. else if (sentence[j] == 'a')
  225. printf("2");
  226. else if (sentence[j] == 'b')
  227. printf("22");
  228. else if (sentence[j] == 'c')
  229. printf("222");
  230. else if (sentence[j] == 'd')
  231. printf("3");
  232. else if (sentence[j] == 'e')
  233. printf("33");
  234. else if (sentence[j] == 'f')
  235. printf("333");
  236. else if (sentence[j] == 'g')
  237. printf("4");
  238. else if (sentence[j] == 'h')
  239. printf("44");
  240. else if (sentence[j] == 'i')
  241. printf("444");
  242. else if (sentence[j] == 'j')
  243. printf("5");
  244. else if (sentence[j] == 'k')
  245. printf("55");
  246. else if (sentence[j] == 'l')
  247. printf("555");
  248. else if (sentence[j] == 'm')
  249. printf("6");
  250. else if (sentence[j] == 'n')
  251. printf("66");
  252. else if (sentence[j] == 'o')
  253. printf("666");
  254. else if (sentence[j] == 'p')
  255. printf("7");
  256. else if (sentence[j] == 'q')
  257. printf("77");
  258. else if (sentence[j] == 'r')
  259. printf("777");
  260. else if (sentence[j] == 's')
  261. printf("7777");
  262. else if (sentence[j] == 't')
  263. printf("8");
  264. else if (sentence[j] == 'u')
  265. printf("88");
  266. else if (sentence[j] == 'v')
  267. printf("888");
  268. else if (sentence[j] == 'w')
  269. printf("9");
  270. else if (sentence[j] == 'x')
  271. printf("99");
  272. else if (sentence[j] == 'y')
  273. printf("999");
  274. else if (sentence[j] == 'z')
  275. printf("9999");
  276. else if (sentence[j] == '1')
  277. printf("1111111111111111");
  278. else if (sentence[j] == '2')
  279. printf("2222");
  280. else if (sentence[j] == '3')
  281. printf("3333");
  282. else if (sentence[j] == '4')
  283. printf("4444");
  284. else if (sentence[j] == '5')
  285. printf("5555");
  286. else if (sentence[j] == '6')
  287. printf("6666");
  288. else if (sentence[j] == '7')
  289. printf("77777");
  290. else if (sentence[j] == '8')
  291. printf("8888");
  292. else if (sentence[j] == '9')
  293. printf("99999");
  294. else if (sentence[j] == '0')
  295. printf("11111111111111111");
  296. else if (sentence[j] == '.')
  297. printf("1");
  298. else if (sentence[j] == '?')
  299. printf("11");
  300. else if (sentence[j] == '!')
  301. printf("111");
  302. else if (sentence[j] == ',')
  303. printf("1111");
  304. else if (sentence[j] == '@')
  305. printf("11111");
  306. else if (sentence[j] == '-')
  307. printf("1111111");
  308. else
  309. printf("#");
  310. }
  311. printf("\n");
  312. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Loopah is offline Offline
60 posts
since Aug 2005
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

Ok, I ran your code through my program in place of mine for the pigLatin() function and it works, but how do I access each word's array? I need to modify each word.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Loopah is offline Offline
60 posts
since Aug 2005
Nov 30th, 2005
0

Re: Scanning an inputted array of words, and separating each word into a new array?

From my earlier mockup:
#include <stdio.h>
#include <string.h>

int main(void)
{
   char sentence[200];
   puts("enter a sentence:");
   fflush(stdout);
   if ( fgets(sentence, sizeof sentence, stdin) != NULL )
   {
      static const char delim[] = " \n";
      size_t i, size = 0;
      char word[10][20], *ptr = strtok(sentence, delim);
      while ( ptr != NULL )
      {
         strcpy(word[size], ptr);
         if ( ++size >= sizeof word / sizeof *word )
         {
            break;
         }
         ptr = strtok(NULL, delim);
      }
      puts("words are:");
      for ( i = 0; i < size; ++i )
      {
         puts(word[i]); /* access to each word */
      }
   }
   return 0;
}

/* my output
enter a sentence:
The quick brown fox jumps over the lazy dog.
words are:
The
quick
brown
fox
jumps
over
the
lazy
dog.
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: project question?
Next Thread in C Forum Timeline: :sad: plz suggest me some solution





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC