Click to See Complete Forum and Search --> : i took the loooong way to code this


optech
06-07-2001, 03:11 PM
i'm new to c (not new to coding though)
and i wanted to make a program that would flip a user's string backwards...
here it is. can i make this smaller?


#include <stdio.h>
main (){
char name1[64];
char name2[64];
int lenn;
int i;
int d = 0;

printf("Please enter your name:\n");
scanf("%s", name1);
lenn=strlen(name1);
i=lenn;
while (i >= 0) {
name2[d]=name1[i];
i--;
d++;
}
i = 0;
while (i <= lenn) {
printf("%c",name2[i]);
i++;
}
printf("\n");
}

TheLinuxDuck
06-07-2001, 03:34 PM
Here is my thought process to getting the job done. Overall, it is a little longer, because I wrote a routine to handle user input better than scanf (evil function).


#include <stdio.h>
//
int getString(const char *text,char *newString, int length)
{
if(text!=NULL) {
printf("%s",text);
fflush(stdout);
}
fgets(newString,length,stdin);
if(newString[strlen(newString)-1]=='\n')
newString[strlen(newString)-1]='\0';
fseek(stdin,0L,SEEK_END);
if(strlen(newString)>0) {
return 1;
}
return 0;
}
//
int main(void)
{
char string1[64],string2[64];
int j,strlength;

if(!getString("String 1:",string1,64)) return 1;

strlength=strlen(string1);
string2[strlength]='\0';
for(j=0;j<strlength;++j)
string2[strlength-j-1]=string1[j];

printf("\nString 1:%s\nString 2:%s\n", string1,string2);
return 0;
}

Here is the code in operation. I also included a string longer than the allowable length to show the truncation of the input function.

13:29:15 Thu Jun 7 root
/home/root/c/optech> ./stringswap
String 1:Finding good people is as easy as looking out your front door.

String 1:Finding good people is as easy as looking out your front door.
String 2:.rood tnorf ruoy tuo gnikool sa ysae sa si elpoep doog gnidniF
13:29:34 Thu Jun 7 root
/home/root/c/optech> ./stringswap
String 1:Maybe times are hard, or maybe they are easy. Always be glad to be wher
e you are.

String 1:Maybe times are hard, or maybe they are easy. Always be glad to
String 2 :ot dalg eb syawlA .ysae era yeht ebyam ro ,drah era semit ebyaM

Strike
06-07-2001, 04:10 PM
I'll hack together a C++ example really quick, because I think I can do this in like 10-12 lines using the STL string class.

TheLinuxDuck
06-07-2001, 04:22 PM
Just because, here it is in perl:

#!/usr/bin/perl -w
use strict;
#
my($string1);
print "Enter a string: ";
chomp($string1=<STDIN> );
print join '', reverse split //, $string1;
print "\n";
exit;

Dru Lee Parsec
06-07-2001, 04:30 PM
How about not using 2 strings. Just one temp char. Here's the psuedo code:


String str = "abcdefg";
for( int i = 0; i < [half the length of str]; i++) {
char temp = str[i];
str[i] = str[str.length() - i];
str[str.length() - i] = temp;
}

// at this point str is reversed

TheLinuxDuck
06-07-2001, 04:36 PM
Oh sure, Dru, just take it to the next level.. sheesh.. (^=

EyesWideOpen
06-07-2001, 04:53 PM
In case anyone cares, here it is in Ruby:


#!/usr/bin/ruby

print "Enter a string: "
string1 = gets

string1 = string1.reverse

print "#{string1}\n"

pinoy
06-07-2001, 05:48 PM
Here's mine



#include <stdio.h>
#include <string.h>

int main( void )
{
char *str = "abcdefg";
char buf[1024], *p = buf, *strp = str + strlen(str)-1;

while (strp >= str)
*p++ = *strp--;
*p = '\0';

printf("String 1: %s\n", str);
printf("String 2: %s\n", buf);

return 0;
}

pinoy
06-07-2001, 05:54 PM
Or using 1 string


#include <stdio.h>
#include <string.h>

int main(void)
{
char str[] = "abcdefg";
char *end = str + strlen(str)-1;
char *start = str;

printf("String 1: %s\n", str);
while (start < end) {
/* swap */
char ch = *start;
*start++ = *end;
*end-- = ch;
}
printf("String 2: %s\n", str);
return 0;
}

TheLinuxDuck
06-07-2001, 05:55 PM
LloydM:

Very slick!! Very slick indeed!

[edit]

Pointers are just too qool...

[ 07 June 2001: Message edited by: TheLinuxDuck ]

sans-hubris
06-07-2001, 08:03 PM
C++ version

#include <iostream>
#include <string>
using namespace std;

int main(void) {
string str;
getline(str, cin);
for(int i=0; i<str.length()/2; i++) {
char end=str.at(str.length()-(i+1));
str.replace(str.length()-(i+1), 1, str.at(i));
str.replace(i, 1, end);
}

cout << str << endl;
return 0;
}

Bradmont2
06-07-2001, 09:08 PM
Originally posted by Strike:
<STRONG>I'll hack together a C++ example really quick, because I think I can do this in like 10-12 lines using the STL string class.</STRONG>

Doesn't the STL string have a reverse() member function?

Bradmont2
06-07-2001, 09:13 PM
Ok... I don't really know much C, so I'll do it in C++ (should be very similar.) Yes, strike, I'm using STL string too... :o


#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;

string rev(string);

int main()
{
string s;
cin &gt;&gt; s;
s = rev(s);
cout &lt;&lt; s;
return 0;
}

string rev(string s)
{
if (s.length() == 0)
return s;

return (s.at(s.length()-1) + rev(s.substr(0, s.length()-1));
}



:D

&lt;edit&gt; BTW, can you recurse main()? If you can, then this could be a very short solution indeed! ;) &lt;/edit&gt;

[ 07 June 2001: Message edited by: Bradmont ]

pinoy
06-08-2001, 04:52 AM
Here's a C++ version.


#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

int main(void)
{
using namespace std;

string s = "abcdefg";
cout &lt;&lt; "String 1: " &lt;&lt; s &lt;&lt; endl;
reverse(s.begin(), s.end());
cout &lt;&lt; "String 2: " &lt;&lt; s &lt;&lt; endl;
return 0;
}

augur
06-08-2001, 05:03 AM
Greetings,

Here's a little C version:


void reverse(char *str) {
int i, j=strlen(str)-1;
for (i=0;i&lt;j;i++,j--)
str[i] ^= str[j] ^= str[i] ^= str[j];
}

Strike
06-08-2001, 11:27 AM
Well crap, LloydM beat me to it :)

pinoy
06-08-2001, 08:01 PM
Sorry Strike. I just noticed everyone were writing STL version and didn't think of using reverse(). Augur's xors are really clever though. Great hack.

Dru Lee Parsec
06-08-2001, 08:35 PM
Sweet! :eek:

I remember that exchange from assembler class YEARS ago;

a = a xor b
b = b xor a
a = a xor b


I wouldn't have thought to put it all in one line using ^= though. That was a sweet piece of code.

[ 08 June 2001: Message edited by: Dru Lee Parsec ]

sans-hubris
06-10-2001, 12:54 AM
Originally posted by LloydM:
<STRONG>Sorry Strike. I just noticed everyone were writing STL version and didn't think of using reverse(). Augur's xors are really clever though. Great hack.</STRONG>
I had realized that after I had already posted the code. By the time I had figured that out, I was too lazy to get back on the computer and post it.