arg2[i] = '\0'shouldn't that be an i++ and the terminating null be added after the while loop?
void invert(const char* src, char* dst)
{
int len = strlen(src);
for (int i = 0; i < len; i++) {
dst[i] = src[len - i - 1];
}
dst[len] = '\0';
}
void invert( char *arg1, char *arg2 )
{
int i = 0;
int len = strlen( arg1 ) - 1;
while( i <= len )
{
arg2[i] = arg1[len - i];
i++;
}
arg2[i] = '\0';
}
irb(main):018:0> "Hello World In Reverse!".reverse
=> "!esreveR nI dlroW olleH"
if(IS_AFFECT(ch, AFF_PERPLEXED ) )
{ char rev[MSL];
invert(txt, rev);
write_to_buffer(ch->desc, rev, 0);
return;
}
class Inverser
def initialize(str)
@original = str
@new = String.new
@colors = Array.new
@color_str = '#'
end
def extract_color
i = 0
get_color = false
@original.each_char do |c|
if get_color == true
@colors[i-1] = (@color_str + c)
get_color = false
elsif c == @color_str
get_color = true
else
@new << c
end
i += 1
end
end
def insert_color
i = 0
arr = Array.new
@new.each_char do |c|
if @colors[i].nil?
arr << c
else
arr << @colors[i]
arr << c
end
i += 1
end
@new = String.new
arr.each do |c|
@new << c
end
end
def reverse_string
arr = Array.new
@new.each_char do |c|
arr << c
end
@new = String.new
arr.size.times do
@new << arr.pop
end
end
def inverse
extract_color
reverse_string
insert_color
return @new
end
end
#include <stdio.h>
#include <string.h>
void invert(const char* src, char* dst)
{ int i, len = strlen(src);
for ( i = 0; i < len; i++)
{ int pos = len - i - 1;
if(src[pos] == '{' && i > 0 && pos < len-1 )
{ dst[i-1] = src[pos];
dst[i] = src[pos+1];
}
else
dst[i] = src[pos];
}
dst[len] = '\0';
}
int main (void)
{ const char *str = "This is my first and only test. Col{rour is a{broun no doubt{";
char inv[200];
invert(str, inv);
printf("%s\n%s\n",str, inv);
return 1;
}
FAIL
#include <stdio.h>
#include <string.h>
void invert(const char* src, char* dst)
{
… SNIPPY SNIP….
}
int main (void)
{ const char *str = "This is my first and only test. Col{rour is a{broun no doubt{";
char inv[200];
invert(str, inv);
printf("%s\n%s\n",str, inv);
return 1;
}