reverse string, how to print string and not decimals?

S

ssecorp

char* reverse(char* str) {
int length = strlen(str);
char* acc[length];
int i;
for (i=0; i<=length-1; i++){
acc[length-1-i] = str;
}
return acc;
}

How do I then print a string and not decimals? If I add printf inside
the for loop it prints decimals. they are reversed so it works but I
want to prin chars.
 
D

Default User

ssecorp said:
char* reverse(char* str) {
int length = strlen(str);
char* acc[length];
int i;
for (i=0; i<=length-1; i++){
acc[length-1-i] = str;
}
return acc;
}

How do I then print a string and not decimals? If I add printf inside
the for loop it prints decimals. they are reversed so it works but I
want to prin chars.


Show us the code. There is no printf() in what you have.




Brian
 
L

Lew Pitcher

char* reverse(char* str) {
int length = strlen(str);
char* acc[length];

acc is an array of pointers to characters. Are you /positive/ that this is
what you want. I don't think it is.
int i;
for (i=0; i<=length-1; i++){
acc[length-1-i] = str;
}
return acc;


Are you /certain/ that you want to return an array of pointers to
characters? Wouldn't you rather return a single pointer to a character?
How do I then print a string and not decimals?
Huh?

If I add printf inside the for loop it prints decimals.

Then you did it wrong. In any case, it makes no sense to put a printf()
inside your reversal loop.
they are reversed so it works but I want to prin chars.

Yes? So?

First off, correct your string reversal function. You want something like...

char *reverse(char *str)
{
char *start, *end, temp;

for (end=str;*end;++end); /* point end at end-of-string */
--end; /* point end at last-character-in-string */
for (start=str; /* point start at first-character-in-string */
start < end; /* loop until start passes end */
++start, --end) /* move start up, end down */
{
temp = *start; /* save the character at start */
*start = *end; /* put character at end into place at start */
*end = temp; /* put saved start character into place at end */
}
return str; /* return pointer to first character */
}

Then, you can do something like...

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char text[] = "This is a test";

printf("Normal: \"%s\"\n",text);
printf("Reversed: \"%s\"\n",reverse(text));

return EXIT_SUCCESS;
}

and see that the reverse() function does indeed reverse any modifiable text
string.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
S

s0suk3

char* reverse(char* str) {
int length = strlen(str);
char* acc[length];
int i;
for (i=0; i<=length-1; i++){
acc[length-1-i] = str;
}
return acc;

}

How do I then print a string and not decimals? If I add printf inside
the for loop it prints decimals. they are reversed so it works but I
want to prin chars.


What do you mean by "decimals"? If you're using printf() like
'printf("%d", str)', then yes, it will print something like
numbers, since the conversion specification '%d' is for integers, not
for chars. Use '%c', or simply putchar().

Also, your function returns a pointer to a local object, which will be
deallocated when the function returns, and thus what it returns will
point to nothing. What you want is probably a function that modifies
the string "in place."

Sebastian
 
J

Jens Thoms Toerring

ssecorp said:
char* reverse(char* str) {
int length = strlen(str);
char* acc[length];

This is problematic for at least four reasons:

a) The size of an array must be a compile-time constant unless
you have a compiler that supports C99 variable length arrays
b) You ask for 'length' pointers to char, but it looks very
much as if you just want an array of chars
c) If 'acc' would be an array of chars it would be too short by
one element if you want to store a string with the same length
as that of 'str' in it (don't forget about the trailing '\0'
char that is needed to make a string out of a mere char array)
d) You later try to pass back a pointer to the array to the caller
but this array is a local variable that vanishes the moment the
function is left and then can't be used anymore.

There are basically two ways you can get aroung the last problem.

1) Have the caller pass a second array to the function in which
you store the reversed string
2) Allocate memory for the reversed string (and tell everyone
that's going to use this function to deallocate it when it's
not needed anymore).

A third approach would be to have a static array that's long
enough for each string you will pass to this function. But there
are two difficulties. First of all knowing in advance what the
longest string that ever will be passed to the function can be
tricky ti say the least. And the reversed string can only be used
between two invokations of the function since the next invokation
will overwrite the previous result.
int i;
for (i=0; i<=length-1; i++){
acc[length-1-i] = str;
}


If 'acc' would be an array of chars (of sufficient length) it
now would contain the reversed chars from 'str'. But it's still
missing a trailing '\0' char and thus it's not a string.
return acc;

And that's, as I wrote, absolutely wrong. You can't safely pass
back a pointer to a local variable for use in the caller.
How do I then print a string and not decimals? If I add printf inside
the for loop it prints decimals. they are reversed so it works but I
want to prin chars.

No idea what this means. Show your printf() call and you probably
will get an answer. But without knowing what exactly you did there
it's impossible to say.
Regards, Jens
 
D

Default User

Mark said:
Default User said:
Mark said:
char* acc[length];

Oops!

I most certainly did NOT write that.

No, but you quoted it,

So? You removed the attribution from the person who DID write it and
said that I did.
and rather than looking
at the code and finding the error, you asked for
additional information, while his problem was
looking you right in the face... no?

No. Without complete code that has a chance of demonstrating his
problem, why waste my time?



Brian
 
B

Barry Schwarz

char* reverse(char* str) {
int length = strlen(str);
char* acc[length];
int i;
for (i=0; i<=length-1; i++){
acc[length-1-i] = str;


Due to what is probably the wrong definition of acc, this statement
contains a constraint violation and the compiler must issue a
diagnostic for it. Did yours? If so, why did you ignore it? If not,
either up the warning level or upgrade to a competent compiler.
}
return acc;
}

How do I then print a string and not decimals? If I add printf inside
the for loop it prints decimals. they are reversed so it works but I
want to prin chars.

You need to tell us what code you executed and how the result differed
from what you want. The fact that you used printf is not sufficient.
Show us the exact code.
 
S

santosh

Default said:
Richard said:
Default User said:
Mark B [Diputsur] wrote:

char* acc[length];

Oops!

I most certainly did NOT write that.

Right - you merely quoted it, and we all do that, obviously.
(Use-mention distinction.)

I fail to understand your point.

I think the point is that the quote markers clearly indicate that you
did not write the "char* acc[length];" line, despite "Mark B
[Diputsur]" deleting the correct attribution line and retaining the
wrong one. So it was, strictly speaking, unnecessary for you to respond
to him, since he wasn't quoting you in the first place.
 
C

Chris M. Thomasson

ssecorp said:
char* reverse(char* str) {
int length = strlen(str);
char* acc[length];
int i;
for (i=0; i<=length-1; i++){
acc[length-1-i] = str;
}
return acc;
}

How do I then print a string and not decimals? If I add printf inside
the for loop it prints decimals. they are reversed so it works but I
want to prin chars.



I have no idea what you want, but here is something you can look at:
________________________________________________________________
#include <stdio.h>
#include <string.h>


static char*
reverse_in_place(
char* const buf
) {
char* spos = buf;
char* epos = spos + strlen(spos) - 1;
for (; spos < epos; ++spos, --epos) {
char const tmp = *spos;
*spos = *epos;
*epos = tmp;
}
return buf;
}


static void
fprint_without_char(
FILE* const out,
char const* buf,
char const strip
) {
for (; *buf; ++buf) {
if (*buf != strip || strip == '\0') {
fputc(*buf, out);
}
}
}


int main() {
char data1[] = "\nsdoirep tuohtiw - ...1 .rev .gnirts .esrever";
char data2[] = "\nsdoirep htiw - ...1 .rev .gnirts .esrever";
fprint_without_char(stdout, reverse_in_place(data1), '.');
fprint_without_char(stdout, reverse_in_place(data2), '\0');
return 0;
}
________________________________________________________________



You can reverse the strings and get the output to strip out the periods via
the `fprint_without_char()' procedure...
 
C

Chris M. Thomasson

[...]
static void
fprint_without_char(
FILE* const out,
char const* buf,
char const strip
) {
for (; *buf; ++buf) {
if (*buf != strip || strip == '\0') {
fputc(*buf, out);
}
}
}

A more "efficient" version of the code above would be:
__________________________________________________________________
static void
fprint_without_char(
FILE* const out,
char const* buf,
char const strip
) {
if (strip) {
for (; *buf; ++buf) {
if (*buf != strip) {
fputc(*buf, out);
}
}
} else {
fprintf(out, "%s", buf);
}
}
__________________________________________________________________

[...]
 
D

Default User

Richard said:
Default User said:

Yes, he did, but...


...no, he didn't do that, not as far as anyone is concerned who
retains the ability to count quote-markers.

I'm sorry, but that's a crock.




Brian
 
D

Default User

santosh said:
Default said:
Richard said:
Default User said:
Mark B [Diputsur] wrote:

char* acc[length];

Oops!

I most certainly did NOT write that.

Right - you merely quoted it, and we all do that, obviously.
(Use-mention distinction.)

I fail to understand your point.

I think the point is that the quote markers clearly indicate that you
did not write the "char* acc[length];" line

I don't believe that quote markers DO adequately indicate that.




Brian
 
M

Mark B [Diputsur]

Default User said:
I'm sorry, but that's a crock.

I apologize if my snip insulted you, but IMHO you
did inherit his mistake with your response.
No. Without complete code that has a chance of demonstrating his
problem, why waste my time?

You had complete code which demonstrated his problem.
The line of code he stuck in for debugging purposes when
the function didn't perform properly sidetracks the issue,
why waste your time asking to see it?
 
D

Default User

Mark said:
I apologize if my snip insulted you,

Learn basic netiquette.
but IMHO you
did inherit his mistake with your response.

Bullshit. I specifically said that I wasn't going to comment because he
didn't provide a complete example.




Brian
 
M

Mark B [Diputsur]

Default User said:
Learn basic netiquette.

**** you. I've been civil this entire time and posting
on usenet (including brief stints on this board) since 1999.
Bullshit. I specifically said that I wasn't going to comment because he
didn't provide a complete example.

Where? What you said was:
"Show us the code. There is no printf() in what you have."

Looks to me like you examined the code, and didn't see a printf()
statement. You're a liar, and an asshole, and I'm done with you.

Good day sir.
 
R

Richard

Default User said:
Learn basic netiquette.


Bullshit. I specifically said that I wasn't going to comment because he
didn't provide a complete example.




Brian

Calm down Bwian. I think you've had your quota of nagging people about
how and what to post today don't you? No wonder Heathfield kicked your
backside about it last year.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top