We want to read a string (max 20 characters) and display it turned around.
So ABCD becomes DCBA!
This should not be difficult, but we tried several things in C, but all didn't work....!!
Please Help!!!
We want to read a string (max 20 characters) and display it turned around.
So ABCD becomes DCBA!
This should not be difficult, but we tried several things in C, but all didn't work....!!
Please Help!!!
What'd you try?
-Govtcheez
[email protected]
We try to Convert a string:
From ABCD --> DCBA
We allready tried this:
#include "..\ownh.h"
#include "..\stdio.h"
int main (void)
{
/* Programm */
char mystring [10] ;
int waarde1 = 10;
char uitk [10];
float mystr [10];
int waarde2;
printf ("Fill in string value: ");
scanf ("%s",mystring);
printf ("String value is: %s\n",mystring);
while (waarde1 > 0)
{
printf ("Converted: %s\n",mystring+waarde1);
waarde1 = waarde1-1;
}
printf ("%s",uitk);
pause ();
return 0;
}
...well gee, did you try the strrev function.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
klausiCode:void stringturn(char *str) { unsigned int front,back; char f,b; back=strlen(str)-1; /* for the strlen() function you must include string.h */ for(front=0;front!=back;front++, back--) { f=*(str+front); b=*(str+back); f^=b; b^=f; f^=b; } }
When I close my eyes nobody can see me...
>...well gee, did you try the strrev function.
strrev isn't a standard function, he may not have it.
Oh yuck, not only does this not work, that XOR trick is really very nasty and should be avoided. Try something along these lines instead:Code:void stringturn(char *str) { unsigned int front,back; char f,b; back=strlen(str)-1; /* for the strlen() function you must include string.h */ for(front=0;front!=back;front++, back--) { f=*(str+front); b=*(str+back); f^=b; b^=f; f^=b; } }
-PreludeCode:/* Pseudocode */ static void revstr ( char *a ) { char b[BUFSIZ] = {0}; int s = 0, e = strlen ( a ) - 1; while ( e >= 0 ) b[s++] = a[e--]; strcpy ( a, b ); }
My best code is written with the delete key.
How do I have to use the strrev option???????
If you want to use the strrev function then you should add the #include <string.h> at the begging of your .c file. The use for the strrev is "strrev (string);".
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Thank you, Prelude for correcting me, but why is my "XOR-trick" "nasty"?
I used it to save the memory for a temporary variable.
Maybe that works:
klausiCode:char *stringturn(char *str) { unsigned int front,back; back=strlen(str)-1; /* for the strlen() function you must include string.h */ for(front=0;front<=back;front++, back--) { *(str+front)^=(str+back); *(str+back)^=(str+front); *(str+front)^=(str+back); } return str; }
Last edited by klausi; 03-30-2002 at 10:32 AM.
When I close my eyes nobody can see me...
Well, thanks for the help,
We will try it tomorrow in school...
We just started programming in C one week ago...
When we have new problems we'll post them :-)
-----
Workgroup CIT Aventus
> When we have new problems we'll post them :-)
NO! Didn't you read.... oh of course you didn't. Silly me. Don't just post your problem, unless you've made an effort on your own, and post your try (IE: code) with your question.
If you get errors that you're working on, post the exact error. We are not the homework committee.
Quzah.
Hope is the first step on the road to disappointment.