strtok() strcpy() and reversing words
When I compile this, the error that I keep getting back is
[I]'temp' may be used uninitialized in this function *temp=next;
and when I run it, it gives me segmentation fault (core dump).
I am testing two different files:
File 1) birds and bees
- the output is supposed to be:
Line: birds and bees
Reversed: bees and birds
File 2)
Line: Now is the time for all good men to come to the aid of their country
Reversed: country their of aid the to come to men good all for time the is Now
Also, I'm not too sure if my logic works out since I haven't been able to run it, so can someone help me out with that.
Code:
#include <stdio.h>
#include <string.h>
#define MAX 79
int main(int argc, char *argv[])
{
char *temp, *first;
int i=10;
char next[i];
char line[MAX];
char line_copy[MAX];
FILE *fp;
//opens files
fp=fopen(argv[1],"r");
//gets and print line
if (fgets(line, MAX, fp)!=NULL)
printf ("Line: ");
puts(line);
printf("Reversed: ");
strcpy(line_copy,line);
//pulling the first substring out
first= strtok(line_copy, " ");
//pulling out substrings after the first
for (i=0;i<=strlen(line);i++){
next[i]=strtok(NULL, " ");
//swapping the words
for (i=strlen(line);i>=0;i--)
temp=0;
*temp=next[i];
next[i]=next[i-1];
next[i-1]= *temp;
printf("%s", next);
}
printf("%s",first);
fclose(fp);
return 0;
}