C reversing a string
1
Name:
Anonymous
2012-10-29 12:00
Why does "printf("%c, reverse")" just put out "aggiN", but ("%s, reverse") outside of the loop put out "aggiNNigga" when it should do the same?
int main(int argc , char* argv [ ] ) {
int i=0;
int c;
int a=0;
char string[]="Nigga";
char reverse[20];
i=sizeof(string);
printf("%d\n", (i-1));
for(c=i-2;c>=0;c--)
{
reverse[a]=string[c];
printf("%c", reverse[a]);
a++;
}
printf("\n%s", reverse);
return 0;
}
2
Name:
Anonymous
2012-10-29 12:10
Use code tags and clean up your disgusting code, ``faggot".
int main(int argc, char* argv [])
{
int strSz = 0;
int i = 0;
int a = 0;
char string[] = "Nigga";
char reverse[20];
strSz = sizeof(string);
printf("%d\n", (strSz - 1));
for(i = strSz - 2; i >= 0; i--)
{
reverse[a] = string[i];
printf("%c", reverse[a]);
a += 1;
}
printf("\n%s", reverse);
return 0;
}
3
Name:
Anonymous
2012-10-29 12:33
char * reverse(const char * in, char * out, size_t out_len){
out[out_len - 1] = 0;
int i = 0;
while(!(ou_str[out_len - 1 - i] = in_str[i]))
++i;
return &(ou_str[out_len - 1 - i]);
}
out must point to a big enough buffer
should be pretty efficient
4
Name:
Anonymous
2012-10-29 12:35
>>3
char * reverse(const char * in, char * out, size_t out_len){
out[out_len - 1] = 0;
int i = 0;
while(!(out[out_len - 1 - i] = in_str[i]))
++i;
return &(out[out_len - 1 - i]);
}
corrected
5
Name:
Anonymous
2012-10-29 12:38
i should be type size_t as well
6
Name:
Anonymous
2012-10-29 13:13
>>2012 `
>not just pushing and popping off the stack
>still using C
ISHYGDDT
7
Name:
Anonymous
2012-10-29 16:46
>>6
I suppose.
void reverse(char * in, char ** out){
if(in[1])
reverse(in + 1, out);
**out++ = *in;
}
8
Name:
Anonymous
2012-10-29 16:48
9
Name:
Anonymous
2012-10-29 16:53
>>6
le /g/ face when le reddit
10
Name:
Anonymous
2012-10-29 16:55
Symta:
reverse [X@Xs]=[@Xs X]
C/C++:
char * reverse(const char * in, char * out, size_t out_len){
out[out_len - 1] = 0;
int i = 0;
while(!(out[out_len - 1 - i] = in_str[i]))
++i;
return &(out[out_len - 1 - i]);
}
11
Name:
Anonymous
2012-10-29 16:56
>>10
Symta:
f !?.??+1 [ø@X]
Haskell:
module Main
where
import List
import Control.Arrow
type Comparator a = (a -> a -> Ordering)
ascending :: (Ord a) => (b -> a) -> Comparator b
ascending f x y = compare (f x) (f y)
descending :: (Ord a) => (b -> a) -> Comparator b
descending = flip . ascending
secondary :: Comparator a -> Comparator a -> Comparator a
secondary f g x y = case f x y of {
EQ -> g x y;
z -> z; }
-- Returns a list of unique elements together with their frequency. Listed in decreasing order of frequency, followed by
increasing order of the elements.
count :: (Ord a) => [a] -> [(a, Int)]
count = map (head &&& length) . sortBy (descending length `secondary` ascending head) . group . sort
main :: IO ()
main = interact $ unlines . map (\(x, y) -> (take 20 $ x ++ repeat ' ') ++ " : " ++ show y) . count . words
12
Name:
Anonymous
2012-10-29 16:56
>>8
How come? Not evaluating in[0] ?
Or
**out++ = *in; instead of
**out = *in;
++out;
13
Name:
Anonymous
2012-10-29 16:59
>>10,11
Have you tried Symta?
14
Name:
Anonymous
2012-10-29 17:01
15
Name:
Anonymous
2012-10-29 17:03
>>11
Haskell:
reverse = foldl (flip (:)) []
16
Name:
Anonymous
2012-10-29 17:10
APL:
⌽
J:
|.
17
Name:
Anonymous
2012-10-29 17:39
>>11
Look into
instance Monoid Ordering.
18
Name:
Anonymous
2012-10-29 17:46
where's the java version of this!?
19
Name:
Anonymous
2012-10-29 17:56
public class Shit{
public static void main(String[] args){
System.out.print("char * reverse(const char * in, char * out, size_t out_len){\nout[out_len - 1] = 0;\nint i = 0;\nwhile(!(out[out_len - 1 - i] = in_str[i]))\n++i;\nreturn &(out[out_len - 1 - i]);}");
}
}
the line breaks are stylistic ofc
20
Name:
Anonymous
2012-10-29 18:16
>>7
void reverse(char* in, char* out, int size) {
out[size] = 0;
rev(in, out+size-1);
}
void rev(char* in, char* out) {
if (*in) rev(in+1, out-1);
*out = *in;
}
21
Name:
Anonymous
2012-10-29 18:24
>>20
that
out[size] = 0; is just asking to
SEGFAULT
and if you put the
*out = *in attribution on top you wouldn't need to keep everything on stack
22
Name:
Anonymous
2012-10-29 18:30
>>21
The size is the one as reported by
strlen(in), so it
shouldn't segfault. Mayhaps a better idea would be
void reverse(char* in, char* out) {
int sz = strlen(in);
out[size] = 0;
rev(in, out+size-1);
}
But this wouldn't exactly be ideal, in my opinion.
You're right about the stack thing, though, it's completely unnecessary. I forgot, is TCO a thing in C compilers or is
gcc the only one that supports it?
void rev(char* in, char* out) {
for (;;) {
*out = *in;
if (*in) { in++; out--; }
else { return; }
}
}
23
Name:
Anonymous
2012-10-29 18:33
>>22
- int sz = strlen(in);
+ int size = strlen(in);
24
Name:
Anonymous
2012-10-29 18:35
>>22
the address of the last element of an array of
x elements is index
x - 1 , not
x
25
Name:
Anonymous
2012-10-29 18:36
>>24
That's not what
strlen returns.
The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).
This should not be confused with the size of the array that holds the string. For example:
char mystr[100]="test string";
defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.
26
Name:
Anonymous
2012-10-29 18:40
>>25
sizeof is not standard C
But yes you're right the string fits perfectly, my mistake
27
Name:
Anonymous
2012-10-29 18:43
Stroustrup > Ritchie
is there any doubt anymore?
28
Name:
Anonymous
2012-10-29 18:44
It's 2012. Put C in the trash. Learn D .
29
Name:
Anonymous
2012-10-29 18:45
>>26
That was an example from
http://www.cplusplus.com/reference/clibrary/cstring/strlen/ . I didn't write it. I'm glad we reached an agreement.
void reverse(char* in, char* out) {
out += strlen(in);
*out-- = '\0';
while (*in) {
*out-- = *in++;
}
}
30
Name:
Anonymous
2012-10-29 19:37
>>26
sizeof is not standard C
The fuck? It's been a standard operator since ANSI.
31
Name:
Anonymous
2012-10-29 20:01
32
Name:
Anonymous
2012-10-29 20:08
*Nigger
33
Name:
Anonymous
2012-10-29 20:36
>>30
I know I had a brain fart, thinking about typeof, I was so embarrassed with the aberration I just said that I closed the computer and went outside
34
Name:
Anonymous
2012-10-30 0:09
35
Name:
Anonymous
2012-10-30 2:38
>>33
I know that feel bro
36
Name:
Anonymous
2012-10-30 12:42
Now in D
string reverse(string str)
{
import std.range;
return retro(str);
}
37
Name:
Anonymous
2012-10-30 18:09
>>36
Go to bed,
Alexandrescu .
38
Name:
Anonymous
2012-10-31 9:21
Someone have the Fortran code to this?
39
Name:
Anonymous
2012-10-31 11:05
>>15
try harder.
Symta:
reverse X = f [??@?] X
Haskell:
reverse = foldl (flip (:)) []
40
Name:
Anonymous
2012-10-31 11:39
>>39
What the fuck are you trying to prove?
Here it is in APL:
⌽
According to your logic, APL is a better language than Symta...?
41
Name:
Anonymous
2012-10-31 11:41
42
Name:
LISPPER
2012-10-31 11:56
43
Name:
Anonymous
2012-10-31 12:01
>>42
APL has an operator for reverse? Terrible
!
44
Name:
Anonymous
2012-10-31 12:02
>>43
If you think about it it's a very common operation, especially if you're doing purely functional tail-recursive stuff.
45
Name:
Anonymous
2012-10-31 12:03
46
Name:
Anonymous
2012-10-31 12:09
>>45
Write a tail-recursive lisp/scheme function that conses each element of a list with its position, i.e.
(a b c) becomes
((a . 0) (b . 1) (c . 2)).
47
Name:
Anonymous
2012-10-31 12:13
Contribooting Javascript.
function reverse(string){
i=string.length;
o="";
while(i>-1){
o+=string.substr(i,1);
i--;
}
return o;
}
48
Name:
Anonymous
2012-10-31 12:18
>>46
yoba N Ys [X@Xs] = yoba N+1 [@Ys [N X]] Xs
;_ Ys _ = Ys> 0
49
Name:
Anonymous
2012-10-31 12:19
>>48
And now in real lisp...
50
Name:
Anonymous
2012-10-31 12:21
>>49
(PROGN
(LOCALLY
(DECLARE (SPECIAL |st|::|glb-yoba|))
(SETF (SYMBOL-FUNCTION '|st|::|glb-yoba|)
((LAMBDA (|st|::|yoba| |st|::|r|)
(PROGN
(SETQ |st|::|yoba|
(NAMED-FN |st|::|yoba|
(|st|::G6273& |st|::G6274& |st|::G6275&)
((LAMBDA (|st|::|u6276&|)
((LAMBDA (|st|::N)
((LAMBDA (|st|::|Ys|)
(IF (XS? |st|::G6275&)
((LAMBDA (|st|::X)
((LAMBDA (|st|::|Xs|)
(FUNCALL |st|::|yoba|
(|st|::|glb-+| |st|::N 1)
(|st|::|glb-suf|
(CL-LST |st|::N |st|::X)
|st|::|Ys|)
|st|::|Xs|))
(|st|::|glb-ltl| |st|::G6275&)))
(|st|::|glb-lhd| |st|::G6275&))
(FUNCALL |st|::|u6276&|)))
|st|::G6274&))
|st|::G6273&))
(LAMBDA ()
((LAMBDA (|st|::|Ys|) |st|::|Ys|) |st|::G6274&)))))
(SETQ |st|::|r| |st|::|yoba|)))
NIL NIL)))
NIL)
51
Name:
Anonymous
2012-10-31 12:21
String.prototype.reverse=function(){
i=this.length;
o="";
while(i>-1){
o+=this.substr(i,1);
i--;
}
return o;
}
Even better Javascript.
"asdf".reverse(); //"fdsa"
faggot="reggin";
faggot.reverse(); //"nigger"
52
Name:
Anonymous
2012-10-31 12:28
>>51
> var strings = ["hello", "world"];
for (var i = 0; i < strings.length; ++i)
console.log(strings[i].reverse());
olleh
olleh
olleh
olleh
olleh
olleh
...
53
Name:
Anonymous
2012-10-31 12:35
>>52
What's wrong with that?
54
Name:
Anonymous
2012-10-31 12:53
>>52
String.prototype.reverse=function(){
this.i=this.length;
this.o="";
while(this.i>-1){
o+=this.substr(this.i,1);
this.i--;
}
return this.o;
}
55
Name:
Anonymous
2012-10-31 12:55
>>54
>o+=this.substr(this.i,1);
Should be
>this.o+=this.substr(this.i,1);
56
Name:
Anonymous
2012-10-31 13:07
(reversen "next!")