Insert a string into a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HansWernerMarschke@web.de

    Insert a string into a string

    Is there a special function to insert a string into another string ?
    For example I want to exchange the german "Umlauts" ä,ö,ü with ae,oe
    and ue in a string.
    I also wonder if there is a function to get the alphabet of a string ?
    Do two strings share the same alphabet ?

    P.S.: What book can you recommand your C and/or C++ ? Don´t say
    Kernighan & Ritchie. Should be useful not only to learn but also to
    look up things.

    Thanks a lot
  • Eric Sosman

    #2
    Re: Insert a string into a string

    HansWernerMarsc hke@web.de wrote:
    Is there a special function to insert a string into another string ?
    For example I want to exchange the german "Umlauts" ä,ö,ü with ae,oe
    and ue in a string.
    No, but there are [1] functions like strchr() and strcspn()
    that can help you find the ä,ö,ü,... in the original string,
    [2] functions like malloc() and realloc() to allocate memory
    for the longer result string, and [3] functions like strcpy(),
    strcat(), memcpy(), and memmove() to copy strings or string
    segments from one place to another.
    I also wonder if there is a function to get the alphabet of a string ?
    Do two strings share the same alphabet ?
    No, but you can do this for yourself easily enough. If
    all you care about is the set of characters from which the
    string is formed (rather than, say, the count of how many
    times each character appears), you could do something like

    unsigned char alphabet[1+UCHAR_MAX];
    memset (alphabet, 0, sizeof alphabet);
    for (ptr = theString; *ptr != '\0'; ++ptr)
    alphabet[ (unsigned char)*ptr ] = 1;

    .... after which the array alphabet[] will contain a 1 in each
    position that corresponds to a character in the string, and a
    0 for each character that is not present. To compare the
    alphabets of two strings, use memcmp() on their arrays.

    Note that this technique assumes one "character" is one
    char, one byte. If multi-byte encodings are a concern you
    will need to work harder.
    P.S.: What book can you recommand your C and/or C++ ?
    Kernighan and Ritchie.
    Don´t say
    Kernighan & Ritchie.
    Oops! Sorry; it just slipped out.
    Should be useful not only to learn but also to
    look up things.
    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • santosh

      #3
      Re: Insert a string into a string

      HansWernerMarsc hke@web.de wrote:

      <snip>
      P.S.: What book can you recommand your C and/or C++ ? Don´t say
      Kernighan & Ritchie. Should be useful not only to learn but also to
      look up things.
      Then I recommend /C: A Reference Manual/ by Harbison and Steele.
      <http://careferencemanu al.com/>

      and /The Standard C Library/ by P. J. Plauger.
      <http://portal.acm.org/citation.cfm?id =532092>

      Comment

      • Richard

        #4
        Re: Insert a string into a string

        santosh <santosh.k83@gm ail.comwrites:
        HansWernerMarsc hke@web.de wrote:
        >
        <snip>
        >
        >P.S.: What book can you recommand your C and/or C++ ? Don´t say
        >Kernighan & Ritchie. Should be useful not only to learn but also to
        >look up things.
        >
        Then I recommend /C: A Reference Manual/ by Harbison and Steele.
        <http://careferencemanu al.com/>
        >
        and /The Standard C Library/ by P. J. Plauger.
        <http://portal.acm.org/citation.cfm?id =532092>
        I would heartily NOT recommend the Standard C Library for a new
        programmer to C. It is more of a reference for when you understand how
        to program in C IMO.

        Comment

        • santosh

          #5
          Re: Insert a string into a string

          Richard wrote:
          santosh <santosh.k83@gm ail.comwrites:
          >
          >HansWernerMarsc hke@web.de wrote:
          >>
          ><snip>
          >>
          >>P.S.: What book can you recommand your C and/or C++ ? Don´t say
          >>Kernighan & Ritchie. Should be useful not only to learn but also to
          >>look up things.
          >>
          >Then I recommend /C: A Reference Manual/ by Harbison and Steele.
          ><http://careferencemanu al.com/>
          >>
          >and /The Standard C Library/ by P. J. Plauger.
          ><http://portal.acm.org/citation.cfm?id =532092>
          >
          I would heartily NOT recommend the Standard C Library for a new
          programmer to C. It is more of a reference for when you understand how
          to program in C IMO.
          The OP seems to want something that he can use as a tutorial *and* as a
          reference. K&R2 is actually very suitable for this purpose (barring the
          fact that's for C90), but he explicitly said he doesn't want it to be
          recommended.

          As an alternative he might try /C Primer Plus/ by Stephen Prata.
          <http://safari.oreilly. com/0672326965>

          It's a good book for learning C and also contains a small reference
          section as an appendix.

          Comment

          • Richard

            #6
            Re: Insert a string into a string

            santosh <santosh.k83@gm ail.comwrites:
            Richard wrote:
            >
            >santosh <santosh.k83@gm ail.comwrites:
            >>
            >>HansWernerMarsc hke@web.de wrote:
            >>>
            >><snip>
            >>>
            >>>P.S.: What book can you recommand your C and/or C++ ? Don´t say
            >>>Kernighan & Ritchie. Should be useful not only to learn but also to
            >>>look up things.
            >>>
            >>Then I recommend /C: A Reference Manual/ by Harbison and Steele.
            >><http://careferencemanu al.com/>
            >>>
            >>and /The Standard C Library/ by P. J. Plauger.
            >><http://portal.acm.org/citation.cfm?id =532092>
            >>
            >I would heartily NOT recommend the Standard C Library for a new
            >programmer to C. It is more of a reference for when you understand how
            >to program in C IMO.
            >
            The OP seems to want something that he can use as a tutorial *and* as a
            reference. K&R2 is actually very suitable for this purpose (barring the
            fact that's for C90), but he explicitly said he doesn't want it to be
            recommended.
            It's the best programming tutorial for beginners I have ever read bar
            none.
            >
            As an alternative he might try /C Primer Plus/ by Stephen Prata.
            <http://safari.oreilly. com/0672326965>
            >
            It's a good book for learning C and also contains a small reference
            section as an appendix.
            Agreed.

            Comment

            • Default User

              #7
              Re: Insert a string into a string

              HansWernerMarsc hke@web.de wrote:

              P.S.: What book can you recommand your C and/or C++ ? D
              First, pick a language. The answers you get (and the newsgroup you
              should be using) will vary.




              Brian

              Comment

              • Keith Thompson

                #8
                Re: Insert a string into a string

                HansWernerMarsc hke@web.de writes:
                Is there a special function to insert a string into another string ?
                For example I want to exchange the german "Umlauts" ä,ö,ü with ae,oe
                and ue in a string.
                I also wonder if there is a function to get the alphabet of a string ?
                Do two strings share the same alphabet ?
                What do you mean by "the alphabet of a string"?
                P.S.: What book can you recommand your C and/or C++ ? Don't say
                Kernighan & Ritchie. Should be useful not only to learn but also to
                look up things.
                There is no book I'd recommend for C and/or C++. There are books I'd
                recommend for C, and books I'd recommend for C++.

                For C, I recommend Kernighan & Ritchie. Knowing why you don't want us
                to mention it (it's widely considered the best book on C, and one of
                the best books on programming) might help us offer more suitable
                advice about other books. Harbison & Steele's "C: A Reference
                Manual", 5th Edition, is a good reference. The standard is the
                definitive reference, but it may not be what you're looking for.

                For C++, ask in comp.lang.c++ -- but first check the C++ FAQ.

                --
                Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                Working...