tags:

views:

162

answers:

2

i made the strrev function myself.while compling it says that the code in the func xstrrev() has no effect.i would also like to know that while making a copy of the built in funtion for assignments can we use builtinfunctions(other) in it?as i used strlen() in it.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void xstrrev(char str[]);
void main(void)
{
 char str[30];
 printf("Enter a string:");
 gets(str);
 xstrrev(str);
 printf("\n%s",str);
 getch();
}
void xstrrev(char str[])
{
int i,x;
x=strlen(str);
for(i=0;;i++)
 {

  if(str[i]=='\0')
  {
   break;
  }

str[x-i]=str[i];

 }
}
+16  A: 

You used the comparison operator == instead of the assignment operator =. So the compiler is right: xstrrev just executes a comparison, whose results are ignored, not an assignment.

As for your second question, this is not the right forum to ask it, only your teacher can tell what is allowed and what is not. However implementing strlen is exactly two lines of code.

EFraim
Also, just remember that strings in C end in null. So if you reverse a string, you don't want the new string to start with null. If we call null '\0', you reversing "abc" you get: `'a''b''c''\0'` should turn into `'c''b''a''\0\'`, not `'\0''c''b''a'`. I hesitate to say this because it's probably a big part of the assignment. This is the kind of little thing that, as a programmer, you have to think about. Not everything that compiles is correct.
glowcoder
+1  A: 

Listen to your compiler's warnings - hopefully there was one about this issue. I got the following:

VS 2010 (MSVC 10), with no particular options about warning levels:

C:\TEMP\test.c(20) : warning C4553: '==' : operator has no effect; did you intend '='?

GCC Version 3.4.5, which unfortunately seems to need some sort of -W option (such as -Wall) set:

C:\TEMP\test.c: In function `xstrrev':
C:\TEMP\test.c:20: warning: statement with no effect
Michael Burr
how did you do this?
fahad
@fahad: It's the output of the compiler. It should show up in the build output window if you're using an IDE and will show up in the console window if you compiling from the command line. Different versions of compilers may need particular switches or options set to get a warning for this particular problem to show. Let us know what tools you're using to compile, and we can give you more exact details on what you need to do to get these diagnostics.
Michael Burr