problem with removing spaces in the string

Discussion in 'C Programming' started by ramu, Sep 19, 2007.

  1. ramu

    ramu Guest

    Hi,

    Suppose I have a string like this:

    "I have a string \"and a inner string\\\" I want to remove space in
    this string but not in the inner string"

    In the above string I have to remove spaces, but not in the inner
    string(\"and a inner string\\\"). Will anyone please tell me how to do
    this?

    regards
    ramu, Sep 19, 2007
    #1
    1. Advertising

  2. ramu wrote:

    > Suppose I have a string like this:
    >
    > "I have a string \"and a inner string\\\" I want to remove
    > space in this string but not in the inner string"
    >
    > In the above string I have to remove spaces, but not in the
    > inner string(\"and a inner string\\\"). Will anyone please
    > tell me how to do this?


    No problem. I suppose you have an input like

    char s[] = "I have a string \"and a inner string\\\" I want"
    " to remove space in this string but not in the "
    "inner string";

    Just start removing spaces while you walk through the string;
    whenever you see a '"' suspend the space removal and when you
    see another '"' resume it. Don't forget to stop at the end of
    the string.

    Ralf
    Ralf Damaschke, Sep 19, 2007
    #2
    1. Advertising

  3. ramu

    osmium Guest

    "ramu" writes:

    > Suppose I have a string like this:
    >
    > "I have a string \"and a inner string\\\" I want to remove space in
    > this string but not in the inner string"
    >
    > In the above string I have to remove spaces, but not in the inner
    > string(\"and a inner string\\\"). Will anyone please tell me how to do
    > this?


    Set a flag indicating you have encountered the first delimiter. Think of one
    or more _states_. Search for information on modes and states and state
    diagrams if you still have trouble.
    osmium, Sep 19, 2007
    #3
  4. ramu

    ramu Guest

    On Sep 19, 12:16 pm, "osmium" <> wrote:
    > "ramu" writes:
    > > Suppose I have a string like this:

    >
    > > "I have a string \"and a inner string\\\" I want to remove space in
    > > this string but not in the inner string"

    >
    > > In the above string I have to remove spaces, but not in the inner
    > > string(\"and a inner string\\\"). Will anyone please tell me how to do
    > > this?

    >
    > Set a flag indicating you have encountered the first delimiter. Think of one
    > or more _states_. Search for information on modes and states and state
    > diagrams if you still have trouble.


    I have done like this:
    char *strrmspace (char *s )
    {
    char *sptr = s;
    char *instr = s;
    char *outstr;
    int i = 0;
    boolean found_quote = FALSE;

    outstr = (char * )malloc(strlen(s)+1);

    while ( *instr != '\0')
    {
    if ( found_quote )
    {
    outstr[i++] = *instr;
    }
    else
    {
    if ( !isspace( *instr) )
    {
    outstr[i++] = *instr;
    }
    }

    if (( *instr == '"') && ( instr[-1] != '\\'))
    {
    found_quote = !found_quote;
    }
    instr++;
    } /* while */
    outstr = '\0';
    (void)strcpy(s, outstr);
    free (outstr);
    return(sptr);
    } /* strrmspace */

    And if I have a string

    char s[] = "I have a string \"\\\" I want to remove space in this
    string but not in the \"inner string\" ";

    But this is removing spaces in \"inner string\" when there is
    backslash in the previous inner string ie. \"\\\".
    I don't want to remove the spaces in the inner strings. How can I
    achieve this?

    regards
    ramu, Sep 19, 2007
    #4
  5. In article <>,
    ramu <> wrote:

    >Suppose I have a string like this:


    >"I have a string \"and a inner string\\\" I want to remove space in
    >this string but not in the inner string"


    >In the above string I have to remove spaces, but not in the inner
    >string(\"and a inner string\\\"). Will anyone please tell me how to do
    >this?


    Incomplete specification.
    - Do inner strings nest?
    - why is the ending delimeter for the inner string \\\" instead
    of \" ? What should happen if a \" appears in the inner string?
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
    Walter Roberson, Sep 19, 2007
    #5
  6. "ramu" <> wrote in message
    news:...
    > On Sep 19, 12:16 pm, "osmium" <> wrote:
    >> "ramu" writes:
    >> > Suppose I have a string like this:

    >>
    >> > "I have a string \"and a inner string\\\" I want to remove space in
    >> > this string but not in the inner string"

    >>
    >> > In the above string I have to remove spaces, but not in the inner
    >> > string(\"and a inner string\\\"). Will anyone please tell me how to do
    >> > this?

    >>
    >> Set a flag indicating you have encountered the first delimiter. Think of
    >> one
    >> or more _states_. Search for information on modes and states and state
    >> diagrams if you still have trouble.

    >
    > I have done like this:
    > char *strrmspace (char *s )
    > {
    > char *sptr = s;
    > char *instr = s;
    > char *outstr;
    > int i = 0;
    > boolean found_quote = FALSE;
    >
    > outstr = (char * )malloc(strlen(s)+1);
    >
    > while ( *instr != '\0')
    > {
    > if ( found_quote )
    > {
    > outstr[i++] = *instr;
    > }
    > else
    > {
    > if ( !isspace( *instr) )
    > {
    > outstr[i++] = *instr;
    > }
    > }
    >
    > if (( *instr == '"') && ( instr[-1] != '\\'))
    > {
    > found_quote = !found_quote;
    > }
    > instr++;
    > } /* while */
    > outstr = '\0';
    > (void)strcpy(s, outstr);
    > free (outstr);
    > return(sptr);
    > } /* strrmspace */
    >
    > And if I have a string
    >
    > char s[] = "I have a string \"\\\" I want to remove space in this
    > string but not in the \"inner string\" ";
    >
    > But this is removing spaces in \"inner string\" when there is
    > backslash in the previous inner string ie. \"\\\".
    > I don't want to remove the spaces in the inner strings. How can I
    > achieve this?
    >
    > regards
    >
    >

    When you encounter a quote, check the previous character to see
    whether that quote is escaped or not.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing
    Fred Kleinschmidt, Sep 19, 2007
    #6
  7. ramu

    osmium Guest

    "ramu" wrote:

    > I have done like this:
    > char *strrmspace (char *s )
    > {
    > char *sptr = s;
    > char *instr = s;
    > char *outstr;
    > int i = 0;
    > boolean found_quote = FALSE;
    >
    > outstr = (char * )malloc(strlen(s)+1);
    >
    > while ( *instr != '\0')
    > {
    > if ( found_quote )
    > {
    > outstr[i++] = *instr;
    > }
    > else
    > {
    > if ( !isspace( *instr) )
    > {
    > outstr[i++] = *instr;
    > }
    > }
    >
    > if (( *instr == '"') && ( instr[-1] != '\\'))
    > {
    > found_quote = !found_quote;
    > }
    > instr++;
    > } /* while */
    > outstr = '\0';
    > (void)strcpy(s, outstr);
    > free (outstr);
    > return(sptr);
    > } /* strrmspace */
    >
    > And if I have a string
    >
    > char s[] = "I have a string \"\\\" I want to remove space in this
    > string but not in the \"inner string\" ";


    See, I don't know what the word this applies to. We are not communicating.
    Furthermore, I don't *want* to know. You have to have rigorous definitions
    to have conversations such as the one you are attempting.

    > But this is removing spaces in \"inner string\" when there is
    > backslash in the previous inner string ie. \"\\\".
    > I don't want to remove the spaces in the inner strings. How can I
    > achieve this?


    The link is not very good but it might help. This is a very simple thing
    but it is almost impossible to describe without the ability to make
    drawings. You have states and there are certain characters, sometimes
    called terminals, that change the state, perhaps the quote or left slash in
    your example. Or perhaps both, I didn't try to follow the details of what
    you want. You can have an unlimited number of states. There is no
    requirement for any kind of symmetry in a state diagram, simple examples
    might give you that impression. You might also look up meta-language. I
    think you are using quote and left backslash as a meta language in your
    posts.


    http://en.wikipedia.org/wiki/State_diagram
    osmium, Sep 19, 2007
    #7
  8. ramu

    ramu Guest

    On Sep 19, 2:13 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    >
    > ramu <> wrote:
    > >Suppose I have a string like this:
    > >"I have a string \"and a inner string\\\" I want to remove space in
    > >this string but not in the inner string"
    > >In the above string I have to remove spaces, but not in the inner
    > >string(\"and a inner string\\\"). Will anyone please tell me how to do
    > >this?

    >
    > Incomplete specification.
    > - Do inner strings nest?
    > - why is the ending delimeter for the inner string \\\" instead
    > of \" ? What should happen if a \" appears in the inner string?
    > --
    > Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


    No. inner strings do not nest. But inner strings can contain one or
    more double quote and/or one or more back slash.
    The delimiter is double quote and \\ is the part of the inner
    string.

    The user input might be like this:

    "I have a string "\\" I want to remove space in
    this "string but not in the" "\"" inner string"

    Regards
    ramu, Sep 19, 2007
    #8
  9. ramu

    Army1987 Guest

    On Wed, 19 Sep 2007 08:56:18 -0700, ramu wrote:

    > Hi,
    >
    > Suppose I have a string like this:
    >
    > "I have a string \"and a inner string\\\" I want to remove space in
    > this string but not in the inner string"
    >
    > In the above string I have to remove spaces, but not in the inner
    > string(\"and a inner string\\\"). Will anyone please tell me how to do
    > this?


    Use a status variable, initialized to (e.g. 0). Loop through the
    source. If a character is whitespace, copy it only if the status
    is 0. If it is a quotation mark, toggle the status, and copy it
    to the target. Else, copy it to the target. A switch statement in
    a loop will do that.
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
    Army1987, Sep 19, 2007
    #9
  10. On Wed, 19 Sep 2007 09:53:54 -0700, ramu <> wrote:

    >On Sep 19, 12:16 pm, "osmium" <> wrote:
    >> "ramu" writes:
    >> > Suppose I have a string like this:

    >>
    >> > "I have a string \"and a inner string\\\" I want to remove space in
    >> > this string but not in the inner string"

    >>
    >> > In the above string I have to remove spaces, but not in the inner
    >> > string(\"and a inner string\\\"). Will anyone please tell me how to do
    >> > this?

    >>
    >> Set a flag indicating you have encountered the first delimiter. Think of one
    >> or more _states_. Search for information on modes and states and state
    >> diagrams if you still have trouble.

    >
    >I have done like this:


    Not a bad first effort.

    >char *strrmspace (char *s )


    Unfortunately, you have chosen a reserved name for your function.

    >{
    > char *sptr = s;
    > char *instr = s;
    > char *outstr;
    > int i = 0;
    > boolean found_quote = FALSE;
    >
    > outstr = (char * )malloc(strlen(s)+1);


    Don't cast the return from malloc. It can never help but can lead to
    silencing a diagnostic you would really want to see.

    >
    > while ( *instr != '\0')
    > {
    > if ( found_quote )
    > {
    > outstr[i++] = *instr;
    > }
    > else
    > {
    > if ( !isspace( *instr) )


    isspace returns true for characters other than ' '.

    > {
    > outstr[i++] = *instr;
    > }
    > }
    >
    > if (( *instr == '"') && ( instr[-1] != '\\'))


    This will cause undefined behavior if a " is the first character of
    the input string.

    > {
    > found_quote = !found_quote;
    > }
    > instr++;
    > } /* while */
    > outstr = '\0';
    > (void)strcpy(s, outstr);


    Why the cast?

    > free (outstr);
    > return(sptr);


    sptr was set to s and neither was ever modified. It looks like you
    don't need sptr and can return s. return is a statement, not a
    function; the parentheses are unnecessary.

    >} /* strrmspace */
    >
    >And if I have a string
    >
    > char s[] = "I have a string \"\\\" I want to remove space in this
    >string but not in the \"inner string\" ";
    >
    >But this is removing spaces in \"inner string\" when there is
    >backslash in the previous inner string ie. \"\\\".
    >I don't want to remove the spaces in the inner strings. How can I
    >achieve this?


    It would avoid ambiguity if you would specify the desired output after
    processing the above array and show us the output your function is
    producing.

    One problem you may think you have is that the \" you see after the
    first word string is not two characters at execution time. It is
    acutely the start of your inner string which continues until the \"
    before the word inner. The phrase inner string is part of your outer
    string and should be stripped.


    Remove del for email
    Barry Schwarz, Sep 20, 2007
    #10
  11. ramu

    runner Guest

    "ramu" <> wrote in message
    news:...
    > Hi,
    >
    > Suppose I have a string like this:
    >
    > "I have a string \"and a inner string\\\" I want to remove space in
    > this string but not in the inner string"
    >
    > In the above string I have to remove spaces, but not in the inner
    > string(\"and a inner string\\\"). Will anyone please tell me how to do
    > this?


    If you still haven't found a solution, please write:

    1) A sample string, as typed by the user.
    2) The same string, according to the C syntax.
    3) The processed string, in C syntax.
    4) The processed string, as written to the console.

    Do you confirm that the START of the *inner string*
    is marked by \" ?

    Do you confirm that the END of the *inner string*
    is marked by \" ?

    Or what?
    runner, Sep 20, 2007
    #11
  12. ramu

    Thad Smith Guest

    ramu wrote:
    > On Sep 19, 2:13 pm, -cnrc.gc.ca (Walter Roberson)
    > wrote:
    >> In article <>,
    >>
    >> ramu <> wrote:
    >>> Suppose I have a string like this:
    >>> "I have a string \"and a inner string\\\" I want to remove space in
    >>> this string but not in the inner string"
    >>> In the above string I have to remove spaces, but not in the inner
    >>> string(\"and a inner string\\\"). Will anyone please tell me how to do
    >>> this?

    >> Incomplete specification.
    >> - Do inner strings nest?
    >> - why is the ending delimeter for the inner string \\\" instead
    >> of \" ? What should happen if a \" appears in the inner string?
    >> --
    >> Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

    >
    > No. inner strings do not nest. But inner strings can contain one or
    > more double quote and/or one or more back slash.
    > The delimiter is double quote and \\ is the part of the inner
    > string.
    >
    > The user input might be like this:
    >
    > "I have a string "\\" I want to remove space in
    > this "string but not in the" "\"" inner string"


    I assume that an inner string has the same syntax as a C string literal.
    If not, please show exactly how they differ. If so, then the inner
    string may represent \ and " characters by escaping them with the
    sequences \\ and \". It is important to note that within string
    literals a series of \s are paired:
    "\\\\"
    is a string literal containing \\, not the beginning of one containing
    an escaped quote.

    I try here to separate the actual characters in an array with the
    representation as a string literal.

    Start with
    <string>the "bad" boys</string>
    having 14 characters, where <string> and </string> are the delimiters of
    a sequence of characters (to reduce confusion).

    C string literal (which is a string of characters) containing those
    characters:
    <string>"the \"bad\" boys"</string>
    has 18 characters.

    A character string containing the above string literal:
    <string>I saw "the \"bad\" boys" yesterday.</string>

    If you give that string as input to your program, I assume you want the
    output to be
    <string>Isaw"the \"bad\" boys"yesterday.</string>,
    but without the outside delimiters.

    You could read the input string shown above, without the outside
    delimiters, from an input stream.

    If you wanted to represent the input as a string literal for testing,
    you would code
    char s[] = "I saw \"the \\\"bad\\\" boys\" yesterday.";

    Your earlier example:
    char s[] = "I have a string \"\\\" I want to remove space in this
    string but not in the \"inner string\" ";

    is a representation of the character sequence
    <string>I have a string "\" I want to remove space in this
    string but not in the "inner string" </string>

    Using my assumed definition of inner string, the inner strings are
    <string>\" I want to remove space in this string but not in the </string>
    and the unterminated inner string
    <string> </string>
    at the very end of the subject string.

    Is that what you intend?

    --
    Thad
    Thad Smith, Sep 21, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John B. Matthews
    Replies:
    4
    Views:
    666
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    620
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,594
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    871
    Jan C. Faerber
    Sep 10, 2009
  5. rsarpi
    Replies:
    1
    Views:
    121
    John W. Krahn
    May 13, 2007
Loading...

Share This Page