Hi all,

I got this problem after reviewing aznballerlee's problem posted some time back and the replies to it.

I need to design the same function but with only one parameter i.e

remove_S(char *p)

I cannot take any local variables within the function.

main looks like this ...

int main()
{
  char str[]="she 'll be a massless princess";
  printf("%s",str);//prints original string
  remove_S(str);
  printf("%s",str);   //prints- he 'll be a male prince
  return;
}

I did got this solution of using recursion as well as using functions like strcpy() or memmove() but again, that implicitly uses more variables.

So can somebody help me out with this one ??

Thanks in advance..

yes, it can be done with just the one pointer into the parameter, assuming the pointer is pointing to the character that needs to be removed.

Hint: you know that the string is null-terminated, so just copy *(ptr+1) to *ptr until *(ptr+1) == NULL. If it is not NULL then increment the pointer and do it again.

Give it a try and post code/questions if you can't get it. That's a pretty good assignment to teach pointers.

The examples in that previous thread use additional variables/functions to do the job. The OP wants to remove the character without using additional variables or functions.

I cannot take any local variables within the function.

I did got this solution of using recursion as well as using functions like strcpy() or memmove() but again, that implicitly uses more variables.

The examples in that previous thread use additional variables/functions to do the job. The OP wants to remove the character without using additional variables or functions.

Does my this solution use more than one variable ?

void remove_s( char tmp[] )
{
    while( *tmp != '\0' )
    {
        if( *tmp == 's' || *tmp == 'S' )
        {
            memmove( tmp, tmp + 1, &tmp[strlen( tmp )] - tmp ) ;
        }
        else
        {
            tmp++ ;
        }
    }
}

I guess I will leave it to someone else to solve his problem since I really dont understand what he is asking for.

>>Does my this solution use more than one variable ?
please read his second statement -- doesn't want to use memmove() or any other functions. I posted an (almost) complete example of how to do it without using any other functions or variables.

Clarification:
1) You do not wish to use ANY local variables at all, so the only variable allowed in the function is the pointer to the string.
2) Using another function is not allowed.
3) You want to remove all 'S' characters, not just the first with this function.
4) Is this homework?

Yep, atleast u got the question right now cause the 3 points are what should be kept in mind.
On your 4 th point , i 'll say Yes and No i.e It has been asked by my teacher but not as homework.

Can u find a way out cause i tried everything and haven't come up with something that implicitly doesn't take up more variables.

Possibly, but it's not obvious. Somewhere you have to keep track of the the number of 'S's removed. The only thing I can think of is using recursion.

Search for an 'S' adding one to the pointer. When you find the 'S', call the function again using the current pointer location. Keep this up until you get to the trailing '\0' then return.

When you return,
copy *ptr+1 to *ptr, increment ptr, and stop when you copied the '\0'
Then return again.

Heh, that was an interesting problem.

> 1) You do not wish to use ANY local variables at all, so the only variable allowed in the function is the pointer to the string.
Check.

> 2) Using another function is not allowed.
Check.

> 3) You want to remove all 'S' characters, not just the first with this function.
Check.

In the words of deep thought "Tricky, but I think I can do it".

Also in the words of deep though "I have an answer, but you're not going to like it".

I've PM'ed it to WaltP for verification.

I've PM'ed it to WaltP for verification.

Yep, it'll work, for strings of a max length. You certainly have a different style than me :mrgreen:

Maybe even I should attempt and post the solution to Professor WaltP.

Yay atlast we get a qualified teacher who balks at void main ( ) :D

Maybe even I should attempt and post the solution to Professor WaltP.

Yay atlast we get a qualified teacher who balks at void main ( ) :D

I don't like your attitude. Spit out that gum and sit in the corner... :twisted:

Hey buddy,

If u really got ur review done from WaltP , can i have the code plz.

I am in a hurry u know !!

Hey buddy,

If u really got ur review done from WaltP , can i have the code plz.

I am in a hurry u know !!

That's not how it's done here. If you want the code, you write it. We will help, but since we don't get your grade, we don't do your homework for you. We don't care how "in a hurry" you are -- cheating is cheating.

And please read this

> i 'll say Yes and No i.e It has been asked by my teacher but not as homework.
So there's no real harm whether you get an answer or not if there are no marks at stake.

What do you get if you show someone elses answer - a pat on the back for having 'leet' google and/or web forum nagging skills?

Post your best effort so far (say you've managed to get it down to just one variable), then perhaps we can suggest what else you can do.

> I am in a hurry u know !!
I'll post it eventually.

Its not about grades nor a pat at the back.

And about my efforts , here are two pieces of code i tried:

//Using Recursion
void remove_s(char *p)
{
  while (*p)
  {
    if(*p=='s')
    {
      remove_s(p+1);
      while(*p)     //code to shift whole string one character.
      {
        *p=*(p+1);
        p++;
      }
      return;
    }
    else
      p++;
  }
}
//using strcpy
void remove_s( char *p )
{
    while(*p)
    {
        if(*p == 's' || *p == 'S')
        {
            strcpy( p, p + 1) ;
        }
        else
        {
            p++ ;
        }
    }
}

Didn't got any other solution and thats what brought me here.

>>What do you get if you show someone elses answer - a pat on the back for having 'leet' google and/or web forum nagging skills?

Its about sharing knowledge and nothing else.
I just wanted to see how its done..

By the way, grow up...
Not everybody who comes knocking here is into homework u know !!

Thanks for all those useless advice though !!

For all those people who are still brooding over the question, here's the solution ...

Any credits for it doesn't go to me though; but to my faculty who had asked the original question..

void remove_S(char *p)
{
while(*p)
{
if(*p=='s'|| *p=='S')
{
*p='\0';
(*p)++;
while(*(p+*p))
{
if(*(p+*p) =='s' || *(p+*p) =='S' )
{
while(*(p+*p))
{
*(p+*p)=*(p+*p+1);
(*p)++;
}
*p='\0';
}
(*p)++;
}
while(*p)
{
*p=*(p+1);
p++;
}
break;
}
p++;
}
return;
}

int main()
{
char str[101];
printf("Enter the string (not more than 100 characters):");
gets(str);
printf("\nThe string is :%s\n",str);
remove_S(str);
printf("The string after the removal of character 's' is :%s\n",str);
return(0);
}

Best use of pointer i would say..
What say u??

For all those people who are still brooding over the question, here's the solution ...

Well, a solution... ;)

Any credits for it doesn't go to me though; but to my faculty who had asked the original question..

My ghod, what a mess! If this is the formatting you are being taught, run fast! No instructor should ever give unformatted code to anyone, especially his students!

Best use of pointer i would say..

What say u??

If you like gobbledy-gook. I will assume it works, but even with formatting there is no way I'm even going to try to follow this code. Too much obfuscation just to prove it can be done. This is a perfect example IMO of "just because it can be done doesn't mean it should be done."

I believe sarcasm was supposed to be out of Daniweb..

Nonetheless...
Since u pointed that..
Here's a formatted and commented version of the code...

try it... u might like it..

void remove_S(char *p)
{
  while(*p)    //Outer loop to traverse the whole string
  {
    if(*p=='s'|| *p=='S') //checking for first occurence of s
    {
      *p='\0';  
      (*p)++; //using the location for traversal
      while(*(p+*p))  //traversing the string without actually moving the pointer
      {
        if(*(p+*p) =='s' || *(p+*p) =='S' )
        {
          while(*(p+*p))
          {
            *(p+*p)=*(p+*p+1);
            (*p)++;
          }
         *p='\0';
        }
        (*p)++;
      }
   while(*p) //shifting the string one character to fill the first occurence location
      {
        *p=*(p+1);
        p++;
      }
      break;
    }
    p++;
  }
  return;
}
int main()
{
  char str[101];
  printf("Enter the string (not more than 100 characters):");
  gets(str);
  printf("\nThe string is :%s\n",str);
  remove_S(str);
  printf("The string after the removal of character 's' is :%s\n",str);
  getch();
  return(0);
}

"My ghod, what a mess! If this is the formatting you are being taught, run fast! No instructor should ever give unformatted code to anyone, especially his students!"

And ya, if i need sarcasm , i would go, listen to my boss.
So please....

>I believe sarcasm was supposed to be out of Daniweb..
What made you think that?

>And ya, if i need sarcasm , i would go, listen to my boss.
What you quoted wasn't sarcasm, it was a legitimate complaint about the readability of what you posted. Since we'd rather assume that you're an intelligent person and know how to post code, the only viable conclusion was that it was given to you poorly formatted.

>try it... u might like it..
Well, it's ugly and rather inelegant, but not a bad solution. I'm honestly not sure I could do better with the restrictions at first glance, but I would have to give it a think to be sure.

while(*(p+*p))

Does that really work :eek: doing pointer math using a character in the string. *p deferences the string, so if it happens to point to the letter 'Z', the result of the pointer math would be *(p + 'Z') or *(p+90) which might attempt to dereference the pointer way beyond the end of the string.

It works because it starts off by doing *p='\0'; to initialise a very small counter to zero.

It's basically using a position in the array as a temporary counter before overwriting it with the modified string.

It does however run into serious problems if the number of characters after the first 's' is more than can be represented in the positive part of a char (say 127 or 255 typically). As soon as that wraps around, then it's bye bye code.

> but to my faculty who had asked the original question..
Is that the same person who told you to use gets() ?

Perhaps you could point out these bugs to your tutor.

Okay here is a simple solution which I have got verified so it should work out to be fine for any length strings...

#include <stdio.h>

void remove_s( char str[] )
{
    while( *str != '\0' ) {
        if( *str == 's' || *str == 'S' ) {
            remove_s( ++str ) ;
            // after returning from the function don't continue with loop
            // this is to avoid the destruction of counter position
            break ;
        }
        else {
            ++str ;
            if( *str == '\0' )
                return ;
        }
    }

    --str ; // take into account the increment which had occured

    // now its a simple thing of shifting chunks
    while( *str != '\0' ) {
        *str = *( str + 1 ) ;
        ++str ;
    }
}

int main( )
{
    char str[] = "She will be a massless princessi" ;
    printf( "\nThe old string: %s", str ) ;
    remove_s( str ) ;
    printf( "\n\nThe new string: %s", str ) ;

    getchar( ) ;
    return (0);
}

Hope it helped, bye.

Hey!maybe he can use a version of my "stripper program" that I just posted? Instead of a file, take in the array pointer and the letter to be stripped,, look at it for the "offending letter" , ignore it when writing back to the array?

This question has already been answered. Start a new discussion instead.