Sunday, December 27, 2009

Can someone modify my coding? I can't fix the errors here?

I'm just a beginner and I want to sort words by manipulating arrays and strings.


Here is my code:





#include%26lt;stdio.h%26gt;


#include%26lt;stdlib.h%26gt;


#include%26lt;string.h%26gt;





void s(char *i[][50], int n)


{


char t[100][50];


int a, b;





for (a = 0; a %26lt; n; ++a)


for(b = 0; b %26lt; n; ++b)


if (strcmp(i[a][50], i[b + 1][50]) %26gt; 0){


t[100][50] = *i[a][50];


*i[a][50] = *i[b + 1][50];


*i[b + 1][50] = t[100][50];


}


}





int main()


{


char j[10000];


char *i[1000][50];


char string[50];


int n, x;





fgets(j, sizeof (j), stdin);


j[strlen(j) - 1] = '\0';





string[50] = strtok(j, 鈥?\t鈥?;


string[strlen(string) - 1] = '\0';


strcpy(*i[0][50] , string[50]);





for (x = 1; (string[50] = strtok(NULL, '; \t';)) != NULL; ++x){


string[strlen(string) - 1] = '\0';


strcpy(*i[x][50] , string[50]);


}





n = x;


s(i, n);


for (x = 0; x %26lt; n; ++x)


printf(';%s\n';, *i);





return 0;


}Can someone modify my coding? I can't fix the errors here?
Fair bits of errors here...


You are comparing the 50st letter of strings that may not be 50 chars long.


Your swap is swapping the 50st character of the strings


Your fgets has no provision for short words entered.


You are setting 0 in the middle of strings that may still be 0...


You are trying to print a pointer (not a char)


etc...


Rewrite your ';code'; in plain english (pseudocode): it will help you see where your mistakes are.

No comments:

Post a Comment