can anyone help me with my exercise.. i want to inverse a string but without using any of string.h' s library functions
can anyone help me with my exercise.. i want to inverse a string but without using any of string.h' s library functions
What have you tried so far? This isn't a homework service. The basic answer is that you have to treat a string for what it is, a char array, and then iterate through the array in reverse.
well i have tried this so far..
Code:void reverse(char str[]) { int i=0, j=0; printf("Enter String (up to 50 chars): "); scanf("%s", str); while(str[j] != '\0') { j++; //j--; } while(i<=j) { char t = str[i]; str[i++] = str[j]; str[j--] = t; } printf("%s", str); return; }
Your attempt doesn't make sense. Ask for and then get the string in main. Then pass that to your reverse function. This is a pretty basic question, search this board for "surname swap", we had a good discussion of it there. Note that since the NULL char evaluates to false for C, you can use that for string length. Additionally, there are methods of getting user input that will return the number of characters read in.
Thanks a lot! Well i have managed to build this part until now but i have to figure a way to count the length of the string without using string.h
Hope is the first step on the road to disappointment.