Lets say I have string...
s = " hello"
I want to shift "hello" to left side by 1 unit. which turns s = "hello".
Here is my code. but its not working. Can anyone look into it and let me know where I am going wrong?
"It's not working" is a poor description of a problem. It forces
us to guess about the nature of the failure: Does the compiler reject
the code, does it crash when it runs, does it run to completion but
produce unexpected results (what are they, and what did you expect), ...
By analogy, you have sent a message to your doctor saying "It hurts,"
but have not told him whether the pain is in your ear or your ankle.
I'll point out some potential problems with your code, but I have
no way of knowing whether any of these is the one bothering you. Next
time, be more informative.
# include <stdio.h>
# include <string.h>
int main(void)
Okay, `s' points to the start of an anonymous array of the
seven characters ' ', 's', 'u', 'r', 'y', 'a', and '\0'. The
fact that the array is not `const' is a historical accident: If
you try to modify the array you invoke undefined behavior.
To get an array of characters that you *can* modify, use
char s[] = " surya";
char *shift(char *, int);
shift(s+1,-1);
printf("%s", s);
If your program's final line of output has no '\n' at the
end, you may or may not ever see it.
As mentioned above, the main() function must return an
`int' value indicating whether the program succeeded or failed,
so there should be a `return some_value;' statement here. When
you "fall off the end" there's no telling what sort of status
your program might report. (Pedantry alert: Recent versions of
the C Standard define "falling off the end" of main() to be
equivalent to ending with `return 0;' -- but those same versions
also forbid the plain `main()' definition you used earlier, so
either way you're in trouble.)
}
char *shift (char *str, int units)
You never use the value returned by the function, so it's
not clear why you return one at all.
{
if (units < 0)
{
for (; *str != '\0'; str++){
*(str + units) = *str;
}
As explained above, it's an error to try to modify the string
your program is actually using. But if you were using a modifiable
string instead, this loop would be equivalent to this sequence of
individual assignments (imagine that a variable `array' points to
the original target of `str' throughout):
array[-1] = array[0]; /* ' ' <- 's' */
array[0] = array[1]; /* 's' <- 'u' */
array[1] = array[2]; /* 'u' <- 'r' */
array[3] = array[4]; /* 'r' <- 'y' */
array[4] = array[5]; /* 'y' <- 'a' */
At this point the assignments stop, because `str' now points at
the final '\0'. The array now holds 'u','r','y','a','a','\0'
and there is an 's' just before it, in the [-1] position. See
the two 'a's? Do you see why the second one is still there?
The returned value isn't used. If you were to use it, you'd
find that it points at the '\0' that terminates the string.
... and what value does the function return if `units' is
zero or positive? You "fall off the end," and if the caller
tries to use the value you failed to return, you get undefined
behavior again.
Next time, explain your problem more fully.