-
menus and strings
I have an assignment to have user enter 10 strings and then create menu to
-print original strings
-print string in ASCII order
-print strings in order of increasing length
-print strings in order of length of first word
This is how far I have gotten, I cannot figure out the last three. Any help would be appreciated.
Thanks,
#include <stdio.h>
int main()
{
char message1[80], message2[80], message3[80], message4[80 message5[80];
char message6[80], message7[80], message8[80], message9[80 message10[80];
int opselect;
printf("Enter a string:\n");
gets(message1);
printf("Enter another string:\n");
gets(message2);
printf("Enter another string:\n");
gets(message3);
printf("Enter another string:\n");
gets(message4);
printf("Enter another string:\n");
gets(message5);
printf("Enter another string:\n");
gets(message6);
printf("Enter another string:\n");
gets(message7);
printf("Enter another string:\n");
gets(message8);
printf("Enter another string:\n");
gets(message9);
printf("Enter another string:\n");
gets(message10);
printf("Enter a select code:");
printf("\n 1 To display all strings input");
printf("\n 2 To display the strings in ASCII order");
printf("\n 3 To display the strings in order of increasing length");
printf)"\n 4 To display the strings in order of the length of the first word in the string");
printf("\n 5 Quit");
scanf("%d", &opselect);
switch (opselect)
{
case 1:
printf("your messages are :\n");
puts(message1);
puts(message2);
puts(message3);
puts(message4);
puts(message5);
puts(message6);
puts(message7);
puts(message8);
puts(message9);
puts(message10);
break;
case 2:
return 0;
}
-
Yikes man! You need to learn what a multidimensional array is, and what a for() loop is!
char messages[NUMBER_OF_MESSAGES][MESSAGE_LENGTH];
int x;
for( x = 0; x < NUMBER_OF_MESSAGES; x++ )
{
... scan message into "messages[x]", or print message
}
You can print the messages like:
printf("%s", messages[x] );
Quzah.
-
Look up the functions in string.h.
-
Thanks,
I think I have the input straightened out. Here is my new code:#include <stdio.h>
int main()
{
char messages[number_of_messages][message_length];
int opselect;
int x;
for( x = 0; x < NUMBER_OF_MESSAGES; x++ )
{
printf("Enter a string:\n");
scanf("%c",&messages[x]);
}
printf("Enter a select code:");
printf("\n 1 To display all strings input");
printf("\n 2 To display the strings in ASCII order");
printf("\n 3 To display the strings in order of increasing length");
printf)"\n 4 To display the strings in order of the length of the first word in the string");
printf("\n 5 Quit");
scanf("%d", &opselect);
switch (opselect)
{
case 1:
for(x=0; x <=10;++i}
printf("\n %s",messages[x]
break;
case 2:
return 0;
}
My next question is this ASCII order? What is this?
Thanks,