So I am writing a program that reverses any given input of strings..
however it looks weird..it produces a new line under the given input then prints the reverse of the string. I am also able to type next to the reversed string which shouldn't happen. I should be able to type in the line under it. Help much appreciated!
Here is a mini example...
Code:
123
321[i can type right next to this output]
Code:
#include <stdio.h>
#define UNLIMITED 10000
int getline(char line[]);
void reverseCopy(char to[], char from[]);
//prints lines without blanks or tabs
//delete lines that are completely blank
main(){
int len; //current line length
char line[UNLIMITED]; //current input line
char newLine[UNLIMITED]; //the new line
//while len is greater than 0
//means there was a line
while ((len = getline(line)) > 0){
reverseCopy(newLine, line);
printf("%s", newLine);
}
}
//copy from into to reversed
void reverseCopy(char to[], char from[]){
int i, revI;
char tmp[100];
i = revI = 0;
while (tmp[i] = from[i] != '\0'){
i++;
}
if (from[i] == '\0'){
//from[i] would equal to '\0'
//so i would have to copy from
//1 less of i
while(i != 0){
to[revI] = from[i - 1];
++revI;
--i;
}
}
to[revI] = '\0';
}
//read a line into s, return length
int getline(char s[]){
int c, i;
for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}