Hi,
I need to read text from file and then as outcome show separate words, but shuffled. I tried few codes but non of these works for me. I'm getting output, but it do not shows words, but some signs only. I'm posting both codes..
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*declare and initialise variable*/
char message[5][20],buffer[20],t[20];
int i=0;
int j;
FILE *file_in;
file_in=fopen("test.txt", "r");
/*stores and prints the data from the string*/
while(fgets(buffer,150,file_in)){
strcpy(message[i],buffer);
}
for (i = 1; i < 5; i++) {
for (j = 1; j < 5; j++) {
if (strcmp(message[j - 1], message[j]) > 0) {
strcpy(t, message[j - 1]);
strcpy(message[j - 1], message[j]);
strcpy(message[j], t);
}
}
}
printf("\nStrings in order are : ");
for (i = 0; i < 5; i++)
printf("%s", message[i]);
getchar();
return 0;
}
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*declare and initialise variable*/
char message[10][150],buffer[150];
int i=0;
int j;
srand(time(NULL));
FILE *file_in;
file_in=fopen("test.txt", "r");
/*stores and prints the data from the string*/
while(fgets(buffer,150,file_in)){
strcpy(message[i],buffer);
}
while(i < 10)
{
j = rand() % 10;
printf("%s\n",message[j]);
i++;
}
return 0;