[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  SMAUG coding
. . -> [Subject]  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] Refresh page


Posted by Saerataga   (24 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (23,003 posts)  [Biography] 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
[Go to top] top

Posted by Saerataga   (24 posts)  [Biography] bio
Date Reply #2 on Sat 04 Mar 2023 03:07 AM (UTC)
Message
Ok thanks for checking, I'll double check my placement.
[Go to top] 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,190 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]