C
Ceriousmall
I'm just putting this out to get some general feed
back..........................
/* 2011 Ceriousmall
This piece of code reverses the character string using the
function reverse(s) */
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
/* assigns the character string to ln[] */
int gotline(char ln[])
{
int c, i;
int at_end;
for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF && c != '\n'; +
+i)
ln = c;
at_end = c == EOF;
if (c == '\n') {
ln = c;
++i;
}
ln = '\0';
if (at_end)
return EOF;
else
return i;
}
/* reverses the character string s[] */
void reverse(char s[])
{
int i, x;
char subline[MAXLINE];
for (x = 0; s[x] != '\0'; ++x)
if (s[x] == '\n') {
i = x;
--i;
}
else
i = x;
for (x = 0; i >= 0; ++x && --i) {
subline[x] = s;
subline = s[x];
}
for (i = 0; i < x; ++i)
s = subline;
}
int main(void)
{
int return_val;
int at_start, at_end;
char line[MAXLINE];
at_start = 0;
while (at_start != EOF) {
return_val = gotline(line);
at_end = return_val == EOF;
reverse(line);
printf("\n%s", line);
putchar('\n');
if (at_end)
at_start = EOF;
}
return 0;
}
I've also found a better way to do the flip on the text stream in
reverse[] but its not
entirely my own idea but i was heading in that general direction,
this was just the nudge i needed.
/* reverses the character string s[] */
void reverse(char s[])
{
int i, x;
char chr;
for (x = 0; s[x] != '\0'; ++x)
if (s[x] == '\n') {
i = x;
--i;
}
else
i = x;
for (x = 0; x < i; ++x && --i) {
chr = s[x];
s[x] = s;
s = chr;
}
}
back..........................
/* 2011 Ceriousmall
This piece of code reverses the character string
function reverse(s) */
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
/* assigns the character string to ln[] */
int gotline(char ln[])
{
int c, i;
int at_end;
for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF && c != '\n'; +
+i)
ln = c;
at_end = c == EOF;
if (c == '\n') {
ln = c;
++i;
}
ln = '\0';
if (at_end)
return EOF;
else
return i;
}
/* reverses the character string s[] */
void reverse(char s[])
{
int i, x;
char subline[MAXLINE];
for (x = 0; s[x] != '\0'; ++x)
if (s[x] == '\n') {
i = x;
--i;
}
else
i = x;
for (x = 0; i >= 0; ++x && --i) {
subline[x] = s;
subline = s[x];
}
for (i = 0; i < x; ++i)
s = subline;
}
int main(void)
{
int return_val;
int at_start, at_end;
char line[MAXLINE];
at_start = 0;
while (at_start != EOF) {
return_val = gotline(line);
at_end = return_val == EOF;
reverse(line);
printf("\n%s", line);
putchar('\n');
if (at_end)
at_start = EOF;
}
return 0;
}
I've also found a better way to do the flip on the text stream in
reverse[] but its not
entirely my own idea but i was heading in that general direction,
this was just the nudge i needed.
/* reverses the character string s[] */
void reverse(char s[])
{
int i, x;
char chr;
for (x = 0; s[x] != '\0'; ++x)
if (s[x] == '\n') {
i = x;
--i;
}
else
i = x;
for (x = 0; x < i; ++x && --i) {
chr = s[x];
s[x] = s;
s = chr;
}
}