My task is this, and I'm having trouble finishing up the code:

  1. Write a function named deleteS that accepts one character pointer as a parameter and returns no value. The parameter is a C string. This function must remove all of the upper and lower case 's' letters from the string. The resulting string must be a valid C string.
    Your function must declare no more than one local variable in addition to the parameter; that additional variable must be of a pointer type. Your function may not use any square brackets.
    int main()
    {
    char msg[50] = "She'll be a massless princess."
    deleteS(msg);
    cout << msg; // prints he'll be a male prince.
    }

Here is what I've managed so far:

void deleteS (char* ptr[])
{
   int*counter;
   for (counter = ptr; counter<ptr + strlen(ptr); counter++)
   {
       if (isupper(*ptr + counter)
          strcpy (" ", i);
       else if (*(ptr + counter) == 's')
               // delete the s, and move any blankspace back

I can't seem to come up with the remaining code. I hear that I can use a vector to delete elements (which is what I want to do), but I haven't learned that yet ..

How would I be able to finish up?

Member Avatar for iamthwee

http://www.daniweb.com/code/snippet262.html

Bit of an over kill but it works if you convert the line to lower case first.

int main(void)
{
   const char before[] = "She'll be a massless princess.";
   printf("before = \"%s\"\n", before);
   test(before, "s", "");
   getchar();
   return 0;
}

The problem is that I can only use one local variable, and not the entire code you provided for me (thouugh that could work)

Member Avatar for iamthwee

The problem is that I can only use one local variable, and not the entire code you provided for me (thouugh that could work)

Yup I agree, that is an overkill. If your function doesn't have to return anything, i.e doesn't have to alter the string, I can see how this would be very easy.

How would it be easy?? I wish for feedback, as this section of the code is due in hours!

Member Avatar for iamthwee

How about using strcat?

I see the idea behind strcat now.
I can show you an idea of what I have ..
it compiles but gives no output ..

#include <iostream>
using namespace std;

void deleteS (char ptr[])
{
    char *counter = ptr;
    for (counter = ptr; counter < ptr + strlen(ptr); counter++)
    {
        if ((*(counter)== 'S') || (*(counter) == 's'))
            continue;
        else
        {    
            strcat(ptr, counter);
            ptr++;
        }
    }
    return;
}

int main()
{
    char msg[50] = "She'll be a massless princess.";
    deleteS(msg);
    cout << msg;  // prints   he'll be a male prince.
}

Both counter and ptr point to the same memory space.. so when you modify ptr in the loop you end up modifying what counter points to.

And btw have you read the description of strcat( ) since it is not doing things in your case whch you are expecting it to do.

You can look into memmove to solve your problem.

My task is this, and I'm having trouble finishing up the code:

Here is what I've managed so far:

void deleteS (char* ptr[])
{
   int*counter;
   for (counter = ptr; counter<ptr + strlen(ptr); counter++)
   {
       if (isupper(*ptr + counter)
          strcpy (" ", i);
       else if (*(ptr + counter) == 's')
               // delete the s, and move any blankspace back

I can't seem to come up with the remaining code. I hear that I can use a vector to delete elements (which is what I want to do), but I haven't learned that yet ..

How would I be able to finish up?

First thing, your function definition should be void deleteS (char* ptr) -- this passes in a pointer, you are passing in an array of pointers.

Next, set up another character pointer. You are not dealing with integers in this function. I'll call this pointer source

Set this new pointer equal to the parameter, like you did in the for statement. Now both pointers point to the same address.

Start a while loop that exits when when you reach the end of source ( *source == '\0' )

In the loop,
1) Copy the current character from source to the current character of ptr.
2) Increment source to point to the next character.
3) If the current character in ptr (the one just copied) is not 's' or 'S', increment ptr to point to the next character. This will overwrite any S that was copied.

When the loop exits, copy the source character to ptr one last time. This loads the ending '\0'. Your string should now be copied, and ptr should now contain no S's.

First thing, your function definition should be void deleteS (char* ptr) -- this passes in a pointer, you are passing in an array of pointers.

That was a typo I think, she has already taken care of this in her last post.

That was a typo I think, she has already taken care of this in her last post.

Noooo, her last post has void deleteS (char ptr[]) and the instructions say
"Your function may not use any square brackets."

IMAO, this includes the definition, and even if it doesn't, the def can still be written without brackets. :)

Anyway, if you notice the times, I was responding while she was posting. It takes me more than 60 seconds to type up a response. ;)

IMAO,...... *a good beating*

Yeah yeah yeah....I really get to see a lot of IMAO nowadays....;)

Anyway, if you notice the times, I was responding while she was posting. It takes me more than 60 seconds to type up a response.

Her post was at 1.30 AM and your answer was at 1.22 PM :twisted:( at my place but still 12 hr difference no matter where you stay)
Maybe Miss Dani should start using a 24 hour clock....:P

I used memmove, but I don't think I can. I can only use Cstrings and pointers. I will try to use the source way (suggested previously) but any ideas how to fix this up so it uses poitners instead?

#include <cstring>
  #include <cstdio>
  using namespace std;
   
  void removeSfromString(char *str)
  {
      unsigned n = strlen(str);
      unsigned i = 0;
   
      while (i < n)
      {
          if (*(str + i) == 's' || *(str + i) == 'S')
          {
              memmove(str + i, str + i + 1, n - i);
              --n;
          }
          else
              ++i;
      }
  }
   
  int main()
  {
      char msg[] = "She'll be a massless princess";
      removeSfromString(msg);
      printf(msg);
      return 0;
  }

You have used two variables in your function and that too not of pointer type which goes against your program requirement.

As far as this program is concerned go with Mr. WaltP's algorithm and it should work out to be fine.

I tried your implementation, Walt P. I got the wrong output. What am I doing wrong?

void removeSfromString(char *str)
{
    // unsigned n = strlen(str);
    char *p = str;

    while (*p != '\0') /* '\0' is the string-terminator character */
    {
        strcpy (str, p);
            p++;
        
        if (*str == 's' || *str == 'S')
        {
            
            str++;
        }
   }

    strcpy(str, p); 
}

int main()
{
    char msg[] = "She'll be a massless princess";
    removeSfromString(msg);
    printf(msg);
    return 0;
}

Copy the current character from source to the current character of ptr.

This doesnt mean to use strcpy( )..you are not dealing with strings here, jsut normal characters. Assign the character at the position source to the character pointed by ptr. *ptr = *src ;

I have this now:

void removeSfromString(char *str)
{
    char *p; 
    p = str;

    while (*p != '\0') // '\0' is the string-terminator character 
    {
        *p = *str;
            p++;
        
        if (*str == 's' || *str == 'S')
        {
            str++;
        }
   }

    *p = *str; 
}

int main()
{
    char msg[] = "She'll be a massless princess";
    removeSfromString(msg);
    cout << msg;
    return 0;
}

and it prints out

ShhhhhhhhhhhhhhhhB

What the ..?:eek:

Sheesh you got confused between the two pointers girly...:D

Here is the modified version, I just swapped p and str and made a change to the condition being checked..

void removeSfromString(char *str)
{
    char *p;
    p = str;

    while (*p != '\0') // '\0' is the string-terminator character
    {
        *str = *p;
        p++;

        if (*str != 's' && *str != 'S')
        {
            str++;
        }
   }
    *str = *p;
}

Hope it helped, bye.

Careless I was :lol:
Thanks for the help you guys, I finally finished my project!

Congratulations, sit back, relax and enjoy your weekend. ;)

Try this,
I think this will solve ur problem.

void delete_S(char *p)
{
  char *q;
  while(p)
  {
    q=p+1;
    (if *p=='s'|| *p=='S')
    {
      while(q)
      {
        *(q-1)=*q;
        q++;
      }
      
      *(q-1)='/0';
    }
     else
    {
       p++;
       q=p+1;
    }
  }

I'm beginning now to wonder of a possible mistake, as I draw out the pointer stuff again.

Since the char array was "She'll be a massless princess", we will have to eliminate the first 'S'.

With this code, I have *str = *p, in the second iteration of the while loop, I replace the 'S' with 'h'. That is right, and as we go on, we get "he'll ..." However, aren't I supposed to allocate a space (empty char) if I have a 'S' or 's'? In this case, I'm not having that ..

or at least not how I have it on paper (but it works on compiler)

void removeSfromString(char *str)
{
    char *p;
    p = str;
 
    while (*p != '\0') // '\0' is the string-terminator character
    {
        *str = *p;
        p++;
 
        if (*str != 's' && *str != 'S')
        {
            str++;
        }
   }
    *str = *p;
}

In other words, if the first char in msg was a 'S', we do not take care of the blank space that we should allocate for it?

so we should have "_he'll be a ...".

(The _ symbolizes the blank space)

I'm beginning now to wonder of a possible mistake, as I draw out the pointer stuff again.

Since the char array was "She'll be a massless princess", we will have to eliminate the first 'S'.

With this code, I have *str = *p, in the second iteration of the while loop, I replace the 'S' with 'h'. That is right, and as we go on, we get "he'll ..." However, aren't I supposed to allocate a space (empty char) if I have a 'S' or 's'? In this case, I'm not having that ..

No, p and str are using the exact same space. You are therefore overwriting the bad characters in the string with characters further on in the string.

Another way to write the exact same code without pointers is:

i = 0;
    j = 0;
    while (str[j] != '\0') // '\0' is the string-terminator character
    {
        str[i] = str[j];
        j++;
 
        if (str[i] != 's' && str[i] != 'S')
        {
            i++;
        }
   }
   str[i] = str[j];

So j is pointing to the next character to move (str in original code), i is pointing to the location where that character is to go (p in original code).

Let's make this more interesting...

Can anyone help me write the code for the same removeSfromString function without taking any local variables ....

i.e now the function is

void removeSfromString(char *str)
{
//no variable declaration here
}

How about something like:

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

int main( )
{
    char* tmp = 0 ;
    char str[] = "Asses are asses and nothing but asses" ;
    printf( "\nActual string: %s", str ) ;
    remove_s( str, tmp ) ;
    printf( "\nNew string: %s", str ) ;
    getchar( ) ;

    return 0 ;
}

And btw...it was not at all interesting..;)

But u changed the whole prototype...taking one more parameter..

Can't we do it as is.. that is with one parameter alone and no extra local variables..??

*sigh* people nowadays demand so much...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main( )
{
    char* tmp = 0 ;
    char str[] = "Ass is an ass and nothing but ass, so be solemn" ;
    printf( "\nActual string: %s", str ) ;
    remove_s( str ) ;
    printf( "\nNew string: %s", str ) ;
    getchar( ) ;

    return 0 ;
}

Let's make this more interesting...

Can anyone help me write the code for the same removeSfromString function without taking any local variables ....

i.e now the function is

void removeSfromString(char *str)
{
//no variable declaration here
}

Why? Is this likely to help the OP? If you want to offer a challenge, start a new thread, don't hijack someone else's thread

Why? Is this likely to help the OP? If you want to offer a challenge, start a new thread, don't hijack someone else's thread

Yes- responses to a thread should be directly related to the original poster's question/problem. Tangential postings only sidetrack the issue.

Let's get back on-topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.