Monday, July 27, 2009

Can anyone of you run and check my c program need to know whats the error?

my program should open a file with a bunch of words and see if its palindrome or not.


example


ada palindrome


back not palindrome


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


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


int main(int argc, char* argv[])


{


FILE *myfileptr;


char * fgets ( char * sc, int num, FILE * words );


char buffer[BUFSIZ];


const char* sc = argv[1];





myfileptr=fopen("words","r");





// gets() should be fgets. Please read the link posted for the function


while(fgets( buffer, sizeof buffer, myfileptr)) {


if(sc)


printf("%s",sc);


printf("EOF\n");


if(strlen(sc)%2==0) printf("Word not palindrom");


else


{


int i;


for(i = 0; i %26lt; strlen(sc); i += 1)


if(sc[i] != sc[strlen(sc) - 1 - i])


{


printf("Word not palindrom");


break;


}





if(i==strlen(sc)) printf("The word is palindrom");


}


}


}


i keep getting


EOF


Bus error

Can anyone of you run and check my c program need to know whats the error?
well...It compiles perfectly here on my window pc.. but when i try to run it. it dose nothing but crash similarly to mines..hum...strange try running this program





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


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


#define EOS '\0'


int main(int argc, char* argv[])


{


FILE *myfileptr;


char * fgets ( char * sc, int num, FILE * words );


char buffer[BUFSIZ];


const char* sc = argv[1];





myfileptr=fopen("words","r");





// gets() should be fgets. Please read the link posted for the function


while(fgets( buffer, sizeof buffer, myfileptr))


{


if(sc)


printf("%s",sc);


printf("EOS\n");


if(strlen(sc)%2==0) printf("Word not palindrom");


else


{


int i;


for(i = 0; i %26lt; strlen(sc); i += 1)


if(sc[i] != sc[strlen(sc) - 1 - i])


{


printf("Word not palindrom");


break;


}





if(i==strlen(sc)) printf("The word is palindrom");


}


}


}





that should do it...good luck bro!!

online survey

No comments:

Post a Comment