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!!!
Printable View
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?
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.
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;
}
}
>...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 );
}
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);".
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;
}
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.