im still a student in computer science, so my code may look a bit bad
my problem is this: i got a code which prints the reverse of a string(eg. input: qwerty output: ytrewq) and when i run it alone in a seperate .c file as the main function, it works fine, but when i include it in my main .c file(which also includes a lot more functions) it goes haywire and prints additional characters in the output.
heres the codeit goes fine alone as the main function but when i put it in my main code which looks like this:Code:char s[100]; int i, j = 0, x = 0; printf("\ns? "); scanf("%s", &s); while(s[x] != '\0') { j++; x++; } i = 0; while(i < j) { s[i] = s[i] + s[j]; s[j] = s[i] - s[j]; s[i] = s[i] - s[j--]; i++; } printf("\nReverse string is :%.2s", s+1);
idk if the second code works because it doesnt realy look like that but i hope you get the point.Code:#include<stdio.h> void reverse(); void main() { int function; printf("Enter function number: "); scanf("%d", &function); switch(function) { case 1: { reverse(); break; } } void reverse() { char s[100]; int i, j = 0, x = 0; printf("\ns? "); scanf("%s", &s); while(s[x] != '\0') { j++; x++; } i = 0; while(i < j) { s[i] = s[i] + s[j]; s[j] = s[i] - s[j]; s[i] = s[i] - s[j--]; i++; } printf("\nReverse string is :%s\n", s+1);; }



LinkBack URL
About LinkBacks



