Terminating a program using carriage return in C -
quesion: write c program accept line of text. store in array & write out backwards. allow length of line unspecified terminated carriage return assume not exceed 80 characters solution:
#include <stdio.h> int main() { printf("\nenter sentence:\n"); char sent[80]; // creates array of length 80 int = 0; while((sent[i] = getchar()) != '\n') { i++; } sent[i] = '\0'; printf("the reversed sentence : "); for(i=i-1; i>=0; i--) { printf("%c", sent[i]); } getchar(); scanf("%c", &sent[i]); return 0; }
is code correct? (i wondering carriage return part)
what if enter 80 characters??? end putting '\0' @ sent[81], bad...
sent[80] '\n' , sent[81] '\0'
also not sure why do
getchar(); scanf("%c",&sent[i]);
at end of function. @ point -1.
Comments
Post a Comment