Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Stripping characters from a string

Stripping characters from a string

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Saerataga   (24 posts)  Bio
Date Fri 03 Mar 2023 11:03 PM (UTC)

Amended on Sat 04 Mar 2023 12:30 AM (UTC) by Nick Gammon

Message
Hi

Anyone know what I'm doing wrong here?
I thought this would work to remove any of these characters from a string.


char     *strip_char(const char *str)
{
        static char ret[MSL];
        char     *retptr;

        retptr = ret;
        for (; *str != '\0'; str++)
        {
          if(*str == '(' || *str == ')' || *str == '<' || *str == '>' || *str == '=' || *str == '-' || *str == '*')
            continue;
                else
                {
                        *retptr = *str;
                        retptr++;
                }
        }
        *retptr = '\0';
        return ret;
}


[EDIT] Code tags added.
Top

Posted by Nick Gammon   Australia  (23,051 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 04 Mar 2023 01:49 AM (UTC)

Amended on Sat 04 Mar 2023 01:51 AM (UTC) by Nick Gammon

Message
Your code does work. I tested it stand-alone thus:


#include <stdio.h>

#define MSL 500

char     *strip_char(const char *str)
{
        static char ret[MSL];
        char     *retptr;

        retptr = ret;
        for (; *str != '\0'; str++)
        {
          if(*str == '(' || *str == ')' || *str == '<' || *str == '>' || *str == '=' || *str == '-' || *str == '*')
            continue;
                else
                {
                        *retptr = *str;
                        retptr++;
                }
        }
        *retptr = '\0';
        return ret;
}

int main ()
  {
  char test [] = "This is a test (round) less: < greater: > equals: = minus: - asterisk: * done";
  printf ("%s\n", strip_char (test));
  return 0;
  }


Compile and run:


$ gcc main.c
$ ./a.out



Output:


This is a test round less:  greater:  equals:  minus:  asterisk:  done


If it appears to be not working, the problem is in the way you are using it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Saerataga   (24 posts)  Bio
Date Reply #2 on Sat 04 Mar 2023 03:07 AM (UTC)
Message
Ok thanks for checking, I'll double check my placement.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


3,883 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.