I'm just putting this out to get some general feed
back..........................
/* 2011 Ceriousmall
This piece of code reverses the character string [s] 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[i] = c;
at_end = c == EOF;
if (c == '\n') {
ln[i] = c;
++i;
}
ln[i] = '\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[i];
subline[i] = s[x];
}
for (i = 0; i < x; ++i)
s[i] = subline[i];
}
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[i];
s[i] = chr;
}
}
|
|
0
|
|
|
|
Reply
|
Ceriousmall
|
3/28/2011 7:24:30 AM |
|
On 03/28/2011 08:24 AM, Ceriousmall wrote:
> I'm just putting this out to get some general feed
> back..........................
Haven't you heard of fgets() or strlen()?
|
|
0
|
|
|
|
Reply
|
Mark
|
3/28/2011 8:13:40 AM
|
|
On 2011-03-28 09:24, Ceriousmall wrote:
> I'm just putting this out to get some general feed
> back..........................
>
> /* 2011 Ceriousmall
> This piece of code reverses the character string [s] using the
> function reverse(s) */
>
> #include<stdio.h>
[...]
Note that there is a much shorter (but slower) solution using recursion:
#include <stdio.h>
void reverse(void)
{
int ch;
ch = getchar();
if (ch != '\n') {
reverse();
putchar(ch);
}
}
int main(void)
{
int ch;
ch = getchar();
while (ch != EOF) {
ungetc(ch, stdin);
reverse();
putchar('\n');
ch = getchar();
}
return 0;
}
/August
--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
|
|
0
|
|
|
|
Reply
|
August
|
3/28/2011 11:24:29 AM
|
|
On 2011-03-28 13:24, August Karlstrom wrote:
[...]
> #include <stdio.h>
>
> void reverse(void)
> {
> int ch;
>
> ch = getchar();
> if (ch != '\n') {
We may want to replace the last line above with
if ((ch != EOF) && (ch != '\n')) {
in order to handle files without newline ending.
/August
--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
|
|
0
|
|
|
|
Reply
|
August
|
3/28/2011 11:40:28 AM
|
|
Ceriousmall <divadsmall@gmail.com> writes:
> I'm just putting this out to get some general feed
> back..........................
>
> /* 2011 Ceriousmall
> This piece of code reverses the character string [s] 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[])
It's much more useful to have a function that can be told how long the
array is with a second parameter.
> {
> int c, i;
> int at_end;
>
> for (i = 0; i < MAXLINE-1 && (c = getchar()) != EOF && c != '\n'; +
> +i)
> ln[i] = c;
> at_end = c == EOF;
>
> if (c == '\n') {
> ln[i] = c;
> ++i;
> }
> ln[i] = '\0';
>
> if (at_end)
> return EOF;
> else
> return i;
The use of at_end seems a little over the top. You could eliminate it
altogether and just end the function with
return c == EOF ? EOF : i;
> }
>
> /* reverses the character string s[] */
This does not do what is says. A reader used to C will expect that all
the character are reversed, but you specifically exclude a terminating
newline.
> void reverse(char s[])
> {
> int i, x;
> char subline[MAXLINE];
>
> for (x = 0; s[x] != '\0'; ++x)
> if (s[x] == '\n') {
> i = x;
> --i;
i = x - 1;
> }
> else
> i = x;
There's no need to set i every time. In fact you should be using
strlen.
>
> for (x = 0; i >= 0; ++x && --i) {
Its clearer to write ++x, --i rather than use &&.
> subline[x] = s[i];
> subline[i] = s[x];
> }
> for (i = 0; i < x; ++i)
> s[i] = subline[i];
> }
>
> 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;
> }
Again, rather too any extra variables for my taste and it's a shame that
the length of the string (which you know form gotline) is not used --
you end up re-scannign it in the reverse function.
<snip>
--
Ben.
|
|
0
|
|
|
|
Reply
|
Ben
|
3/28/2011 12:39:49 PM
|
|
Thanks for the feed back guys............. I even missed a few
elementary things.............
|
|
0
|
|
|
|
Reply
|
Ceriousmall
|
3/28/2011 2:50:04 PM
|
|
On March 28, 2011 03:24, in comp.lang.c, divadsmall@gmail.com wrote:
> I'm just putting this out to get some general feed
> back..........................
>
> /* 2011 Ceriousmall
> This piece of code reverses the character string [s] 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[i] = c;
> at_end = c == EOF;
>
> if (c == '\n') {
> ln[i] = c;
> ++i;
> }
> ln[i] = '\0';
>
> if (at_end)
> return EOF;
> else
> return i;
> }
>
> /* reverses the character string s[] */
> void reverse(char s[])
[snip]
void reverse(char s[])
char *front, *back;
/* find the end of the string */
for (back = s; *back != 0; ++back) {/*nop*/}
/* swap front with back, move both toward middle */
for (front = s; front+1 < back; ++front, --back)
{
char temp;
temp = *(back-1);
*(back-1) = *front;
*front = temp;
}
}
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------
|
|
0
|
|
|
|
Reply
|
lpitcher2 (869)
|
3/29/2011 1:14:54 AM
|
|
Oops.... copy&paste error
On March 28, 2011 21:14, in comp.lang.c, lpitcher@teksavvy.com wrote:
> On March 28, 2011 03:24, in comp.lang.c, divadsmall@gmail.com wrote:
>
>> I'm just putting this out to get some general feed
>> back..........................
>>
>> /* 2011 Ceriousmall
>> This piece of code reverses the character string [s] 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[i] = c;
>> at_end = c == EOF;
>>
>> if (c == '\n') {
>> ln[i] = c;
>> ++i;
>> }
>> ln[i] = '\0';
>>
>> if (at_end)
>> return EOF;
>> else
>> return i;
>> }
>>
>> /* reverses the character string s[] */
>> void reverse(char s[])
> [snip]
void reverse(char s[])
{
char *front, *back;
/* find the end of the string */
for (back = s; *back != 0; ++back) {/*nop*/}
/* swap front with back, move both toward middle */
for (front = s; front+1 < back; ++front, --back)
{
char temp;
temp = *(back-1);
*(back-1) = *front;
*front = temp;
}
}
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------
|
|
0
|
|
|
|
Reply
|
lpitcher2 (869)
|
3/29/2011 1:16:51 AM
|
|
|
7 Replies
202 Views
(page loaded in 0.107 seconds)
Similiar Articles: find/replace line of code (with carriage return) - comp.soft-sys ...... file in which I need to find 1 line of code, then ... I was trying to just use '\n' within the search and replace string in ... not to allow for some special characters ... improve strlen - comp.lang.asm.x86There is just so much string ... HLLs, you need to move beyond strlen. As an aside: A few years back (okay, maybe decades at this point) some ... you can feed your code ... To display a 3D skeleton, what do I need to learn? - comp.graphics ...... of the red book before I come back to try to understand the code in these links? What do I need ... I'm just saying that if it was me ... you have it in memory ready to feed ... Parsing file names with spaces - comp.lang.perl.misc... then please attempt to exert some self control and don't feed it! ... universally portable, most people just need ... parsing alpha and numeric characters out of string - comp ... Can we convert a char to ascii in awk - comp.lang.awk... an ASCII value to its corresponding character.. How could we get the reverse case.. ... XGAWK just makes this all a lot more ... the table as you call the function give back ... how to assign a NaN? - comp.lang.fortranIt is a perfectly normal set of 3 characters. It > just ... had many other simillar experiences, some dating back ... I have seen systems where some character bit patterns ... convert integer to string - comp.lang.perl.miscI thought, "No problem, I'll just create a ... Note that this code does some other things and c ... string, e.g. leading zeros, then you need to treat that string as ... nptq -p slowness - comp.protocols.time.ntpNevertheless this is just a string. The only thing that the ntpq code does ... which normally is a 4-character string. Some ... certain circumstances quite some time back ... perl + regex bug? - comp.lang.perl.misc... the problem was a bug. basically, i have some code ... in some of those orbitals and then spit some of them back ... expressions, is it possible to split a string based on some ... How best to detect duplicate values in a column? - comp.databases ...But some Latin-1 characters show just fine but don't transmit ... or others here I appended the string that comes back ... need ever arises) what other code would need to ... @codemonkeyism: Java Interview questions: Write a String Reverser ...Most candidates will need some time and some ... /* The following code works fine in reverse the string without the stack ... I’d like to ask you to step back just for a ... algorithm - Best way to reverse a string in C# 2.0 - Stack Overflow... code sample will correctly reverse a string that contains non-BMP characters ... Need to use this overload for the System.String constructor // as providing just ... some ... 7/25/2012 11:31:51 PM
|