How to remove and substitute characters within a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • francescomoi@europe.com

    How to remove and substitute characters within a string

    Hi.

    I'm trying to remove some characters within a string and substitute
    others. For instance, I want to convert:
    John's new house, great ---> Johns-new-house-great

    I tried with:
    ----------------//------------------
    int main()
    {
    char string2[50];
    char *string = "John's new house, great";
    int rc = sscanf(string, "%[, ]%s", string2);
    if (rc = 2)
    {
    printf("%s\n", string2);
    }
    return 0;
    }
    -------------//---------------

    But it doesn't work. Any suggestion? Thx.

  • lndresnick@gmail.com

    #2
    Re: How to remove and substitute characters within a string


    francesco...@eu rope.com wrote:[color=blue]
    > Hi.
    >
    > I'm trying to remove some characters within a string and substitute
    > others. For instance, I want to convert:
    > John's new house, great ---> Johns-new-house-great
    >
    > I tried with:
    > ----------------//------------------
    > int main()
    > {
    > char string2[50];
    > char *string = "John's new house, great";
    > int rc = sscanf(string, "%[, ]%s", string2);
    > if (rc = 2)
    > {
    > printf("%s\n", string2);
    > }
    > return 0;
    > }
    > -------------//---------------
    >
    > But it doesn't work. Any suggestion? Thx.[/color]

    Perhaps you want something more like this:

    #include <stdio.h>

    int main(void)
    {
    char string2[50];
    const char *string = "John's new house, great";
    char *p = string2;

    for (; *string != '\0'; string++) {
    switch(*string) {
    case '\'':
    case ',':
    continue; /* eliminate ' */
    case ' ':
    *p++ = '-';
    break;
    default:
    *p++ = *string;
    break;
    }
    }

    *p = '\0';

    printf("%s\n", string2);

    return 0;
    }

    Comment

    • Gregory Pietsch

      #3
      Re: How to remove and substitute characters within a string

      A couple of suggestions:

      1)

      #include <ctype.h>
      #include <stdio.h>

      int main(void)
      {
      char *s = "John's new house, great";
      char *p = s;

      while (*p) {
      if (ispunct(*p))
      ; /* do nothing */
      else if (isspace(*p))
      putchar('-');
      else
      putchar(*p);
      p++;
      }
      return 0;
      }

      2)

      Go get FreeDOS edlin, which contains a dynamic string library that has
      functions that deal with this type of string-scanning problem.

      Gregory Pietsch

      Comment

      • Peter Nilsson

        #4
        Re: How to remove and substitute characters within a string

        Gregory Pietsch wrote:[color=blue]
        > ...
        > #include <ctype.h>
        > #include <stdio.h>
        >
        > int main(void)
        > {
        > char *s = "John's new house, great";
        > char *p = s;[/color]

        More robust is...

        const char *s = "John's new house, great";
        const unsigned char *p = (const unsigned char *) s;
        [color=blue]
        >
        > while (*p) {
        > if (ispunct(*p))
        > ; /* do nothing */
        > else if (isspace(*p))
        > putchar('-');
        > else
        > putchar(*p);
        > p++;
        > }
        > return 0;
        > }[/color]

        --
        Peter

        Comment

        • Stan Milam

          #5
          Re: How to remove and substitute characters within a string

          francescomoi@eu rope.com wrote:[color=blue]
          > Hi.
          >
          > I'm trying to remove some characters within a string and substitute
          > others. For instance, I want to convert:
          > John's new house, great ---> Johns-new-house-great
          >
          > I tried with:
          > ----------------//------------------
          > int main()
          > {
          > char string2[50];
          > char *string = "John's new house, great";
          > int rc = sscanf(string, "%[, ]%s", string2);
          > if (rc = 2)
          > {
          > printf("%s\n", string2);
          > }
          > return 0;
          > }
          > -------------//---------------
          >
          > But it doesn't work. Any suggestion? Thx.
          >[/color]

          Try this:

          /*************** *************** *************** *************** **********/
          /* File Id. chrsubst.c. */
          /* Author: Stan Milam. */
          /* Date Written: 17 Apr. 1992. */
          /* Description: */
          /* Implement a function to search and replace characters in a C */
          /* string. */
          /* (c) Copyright 2005 by Stan Milam. */
          /* All rights reserved. */
          /* */
          /*************** *************** *************** *************** **********/

          #include <stddef.h>
          #include <string.h>

          /* $Revision$ */
          extern char tb_copyright[];
          static char *copyright = tb_copyright;

          /*************** *************** *************** *************** **********/
          /* Name: */
          /* chrsubst(). */
          /* */
          /* Description: */
          /* This function will replace all occurances of one character */
          /* with another in a character string. It will then return */
          /* the number of substitutions. */
          /* */
          /* Arguments: */
          /* char *s1 - String to be searched. */
          /* char ch - Character to search for. */
          /* char ch2 - Character to replace the value of ch. */
          /* */
          /* Returns: */
          /* The number of times ch is in s1. */
          /* */
          /*************** *************** *************** *************** **********/

          size_t
          chrsubst(char *s1, int ch, int ch2) {

          size_t count = 0; /* The count to return */
          char *wrk = strchr(s1, ch); /* Find first char in s1 */

          while (wrk) { /* While we have matches */
          *wrk = (char) ch2; /* Replace the character */
          count++, wrk++; /* Increment the count & pointer */
          wrk = strchr(wrk, ch); /* Search for next occurance */
          }
          return count; /* Return the count */
          }

          /*************** *************** *************** *************** **********/
          /* Name: */
          /* substitute_char (). */
          /* */
          /* Description: */
          /* Alias of the chrsubst() function with a more english-like */
          /* name. */
          /* */
          /*************** *************** *************** *************** **********/

          size_t
          substitute_char ( char *s, int ch1, int ch2 )
          {
          return chrsubst( s, ch1, ch2 );
          }

          Comment

          Working...