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.

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?

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?

Post your attempt. The basics are something like this:

static const char delim[] = " \n";
      size_t 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);
      }

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?

#include <stdio.h>
#include <string.h>

void nameGame();
void pigLatin();
void changeLatin(char [], int);
void cellPhone();
void cellButtons(char [], int);

main()
{
	int input = 0;

	printf("\n\tEnter your choice:\n\t1. Name Game\n\t2. Pig Latin\n\t3. Text Messaging\n\t4. Exit\n");

	scanf("%d", &input);

	while (input != 4)
	{
		if (input == 1)
			nameGame();
		else if (input == 2)
			pigLatin();
		else if (input == 3)
			cellPhone();
		else
			printf("Invalid choice.  Enter a number 1-4.");

		printf("\n\tEnter your choice:\n\t1. Name Game\n\t2. Pig Latin\n\t3. Text Messaging\n\t4. Exit\n");
		scanf("%d", &input);
	}

	return 0;

}

void nameGame()
{
	char c, *stringVowel, firstName[25];
	const char *stringVowels = "aeiou";
	int i = 0;
	puts("Enter a first name: ");

	while ( ( c = getchar() ) != '\n')
		firstName [i++] = c;

	firstName[i] = '\0';

	stringVowel = strpbrk(firstName, stringVowels);

	if (firstName[0] == 'B' || firstName[0] == 'b')
	{
		printf("\n%s!\n", firstName);
		printf("%s, %s bo %s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel),
		strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
	}

	else if (firstName[0] == 'F' || firstName[0] == 'f')
	{
		printf("\n%s!\n", firstName);
		printf("%s, %s bo B%s Bonana fanna fo %s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel),
		strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
	}

	else if (firstName[0] == 'M' || firstName[0] == 'm')
	{
		printf("\n%s!\n", firstName);
		printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo %s, %s!", firstName, firstName, strstr(firstName, stringVowel),
		strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
	}

	else if (firstName[0] == 'A' || firstName[0] == 'a')
	{
		firstName[0] = 'a';
		printf("\n%s!\n", firstName);
		printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
		firstName, firstName, firstName);
	}

	else if (firstName[0] == 'E' || firstName[0] == 'e')
	{
		firstName[0] = 'e';
		printf("\n%s!\n", firstName);
		printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
		firstName, firstName, firstName);
	}

	else if (firstName[0] == 'I' || firstName[0] == 'i')
	{
		firstName[0] = 'i';
		printf("\n%s!\n", firstName);
		printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
		firstName, firstName, firstName);
	}

	else if (firstName[0] == 'O' || firstName[0] == 'o')
	{
		firstName[0] = 'o';
		printf("\n%s!\n", firstName);
		printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
		firstName, firstName, firstName);
	}

	else if (firstName[0] == 'U' || firstName[0] == 'u')
	{
		firstName[0] = 'u';
		printf("\n%s!\n", firstName);
		printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName,
		firstName, firstName, firstName);
	}

	else
	{
	printf("\n%s!\n", firstName);
	printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel),
	strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName);
	}
}

void pigLatin()
{
	char c, d, sentence[200], tempSentence[200], *wordPtr;
	int i = 0, j = 0;
	puts("Enter a sentence: ");

	while ( ( c = getchar() ) != '\n')
		sentence [i++] = c;

	sentence[i] = '\0';

	wordPtr = strtok(sentence, " ");

	i = 0;

	while (wordPtr != NULL)
	{
		printf("%s\t", wordPtr);

		while ( ( d = getchar() ) != '\0')
		tempSentence [i++] = d;

		printf("%s\n", tempSentence);
	
		wordPtr = strtok(NULL, " ");
	}
}

void changeLatin(char sentence[200], int i)
{

}
void cellPhone()
{
	char c, sentence[200];
	int i = 0, j = 0;
	puts("Enter a sentence: ");

	while ( ( c = getchar() ) != '\n')
		sentence [i++] = c;

	sentence[i] = '\0';

	cellButtons(sentence, i);
}

void cellButtons(char sentence[], int i)
{
	int j = 0;

	for(j = 0; j < i; j++)
	{
		if (sentence[j] == 'A')
			printf("*2");
		else if (sentence[j] == 'B')
			printf("*22");
		else if (sentence[j] == 'C')
			printf("*222");
		else if (sentence[j] == 'D')
			printf("*3");
		else if (sentence[j] == 'E')
			printf("*33");
		else if (sentence[j] == 'F')
			printf("*333");
		else if (sentence[j] == 'G')
			printf("*4");
		else if (sentence[j] == 'H')
			printf("*44");
		else if (sentence[j] == 'I')
			printf("*444");
		else if (sentence[j] == 'J')
			printf("*5");
		else if (sentence[j] == 'K')
			printf("*55");
		else if (sentence[j] == 'L')
			printf("*555");
		else if (sentence[j] == 'M')
			printf("*6");
		else if (sentence[j] == 'N')
			printf("*66");
		else if (sentence[j] == 'O')
			printf("*666");
		else if (sentence[j] == 'P')
			printf("*7");
		else if (sentence[j] == 'Q')
			printf("*77");
		else if (sentence[j] == 'R')
			printf("*777");
		else if (sentence[j] == 'S')
			printf("*7777");
		else if (sentence[j] == 'T')
			printf("*8");
		else if (sentence[j] == 'U')
			printf("*88");
		else if (sentence[j] == 'V')
			printf("*888");
		else if (sentence[j] == 'W')
			printf("*9");
		else if (sentence[j] == 'X')
			printf("*99");
		else if (sentence[j] == 'Y')
			printf("*999");
		else if (sentence[j] == 'Z')
			printf("*9999");
		else if (sentence[j] == 'a')
			printf("2");
		else if (sentence[j] == 'b')
			printf("22");
		else if (sentence[j] == 'c')
			printf("222");
		else if (sentence[j] == 'd')
			printf("3");
		else if (sentence[j] == 'e')
			printf("33");
		else if (sentence[j] == 'f')
			printf("333");
		else if (sentence[j] == 'g')
			printf("4");
		else if (sentence[j] == 'h')
			printf("44");
		else if (sentence[j] == 'i')
			printf("444");
		else if (sentence[j] == 'j')
			printf("5");
		else if (sentence[j] == 'k')
			printf("55");
		else if (sentence[j] == 'l')
			printf("555");
		else if (sentence[j] == 'm')
			printf("6");
		else if (sentence[j] == 'n')
			printf("66");
		else if (sentence[j] == 'o')
			printf("666");
		else if (sentence[j] == 'p')
			printf("7");
		else if (sentence[j] == 'q')
			printf("77");
		else if (sentence[j] == 'r')
			printf("777");
		else if (sentence[j] == 's')
			printf("7777");
		else if (sentence[j] == 't')
			printf("8");
		else if (sentence[j] == 'u')
			printf("88");
		else if (sentence[j] == 'v')
			printf("888");
		else if (sentence[j] == 'w')
			printf("9");
		else if (sentence[j] == 'x')
			printf("99");
		else if (sentence[j] == 'y')
			printf("999");
		else if (sentence[j] == 'z')
			printf("9999");
		else if (sentence[j] == '1')
			printf("1111111111111111");
		else if (sentence[j] == '2')
			printf("2222");
		else if (sentence[j] == '3')
			printf("3333");
		else if (sentence[j] == '4')
			printf("4444");
		else if (sentence[j] == '5')
			printf("5555");
		else if (sentence[j] == '6')
			printf("6666");
		else if (sentence[j] == '7')
			printf("77777");
		else if (sentence[j] == '8')
			printf("8888");
		else if (sentence[j] == '9')
			printf("99999");
		else if (sentence[j] == '0')
			printf("11111111111111111");
		else if (sentence[j] == '.')
			printf("1");
		else if (sentence[j] == '?')
			printf("11");
		else if (sentence[j] == '!')
			printf("111");
		else if (sentence[j] == ',')
			printf("1111");
		else if (sentence[j] == '@')
			printf("11111");
		else if (sentence[j] == '-')
			printf("1111111");
		else
			printf("#");		
	}
	printf("\n");
}

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.

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.
*/

Oh nice! Any idea on why my program gets thrown into an error when I run it with the while loop in main()?

Hmm, that is they way we were always taught to get input. Not sure of another way to get input. That way has always worked in the past.

Thank you very much for your help! I appreciate it. You guys here are very helpful, and I hope to do my part in the helping soon as well.

This article has been dead for over six months. Start a new discussion instead.