String appending

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Selvesteen

    #1

    String appending

    Hello Gurus,

    I need to append a sub-string at the start of strings using C. What is
    the best way to achieve that?

    for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
    result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...

    Advance thanks for all your help.

  • Suman

    #2
    Re: String appending

    Selvesteen wrote:[color=blue]
    > Hello Gurus,
    >
    > I need to append a sub-string at the start of strings using C. What is
    > the best way to achieve that?
    >
    > for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
    > result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...
    >
    > Advance thanks for all your help.[/color]
    <from code posted earlier, by W Roberson, in recent past,
    some other thread, "#defines and strings" ...>

    /*--BEGIN--*/

    #include <stdio.h>

    #define PASTE(T1,T2) T1##T2
    #define Append(N) (N, PASTE("SSL_",N) )

    int main()
    {
    printf("%s\n", Append("CLASS") );
    return 0;
    }
    /*--END--*/

    Does that help?

    Comment

    • Jirka Klaue

      #3
      Re: String appending

      Suman:[color=blue]
      > Selvesteen:[/color]
      ....[color=blue][color=green]
      >> for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
      >> result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...[/color][/color]
      ....[color=blue]
      > /*--BEGIN--*/
      >
      > #include <stdio.h>
      >
      > #define PASTE(T1,T2) T1##T2
      > #define Append(N) (N, PASTE("SSL_",N) )
      >
      > int main()
      > {
      > printf("%s\n", Append("CLASS") );
      > return 0;
      > }
      > /*--END--*/
      >
      > Does that help?[/color]

      Most probably not. This is not valid C and shouldn't compile.
      And what is the 'N,' in Append(N) for?

      #include <stdio.h>

      #define APPEND(x) ("SSL_" #x)

      int main()
      {
      printf("%s\n", APPEND(CLASS));
      return 0;
      }

      Jirka

      Comment

      • Suman

        #4
        Re: String appending



        Jirka Klaue wrote:[color=blue]
        > Suman:[color=green]
        > > Selvesteen:[/color]
        > ...[color=green][color=darkred]
        > >> for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
        > >> result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...[/color][/color]
        > ...[color=green]
        > > /*--BEGIN--*/
        > >
        > > #include <stdio.h>
        > >
        > > #define PASTE(T1,T2) T1##T2
        > > #define Append(N) (N, PASTE("SSL_",N) )
        > >
        > > int main()
        > > {
        > > printf("%s\n", Append("CLASS") );
        > > return 0;
        > > }
        > > /*--END--*/
        > >
        > > Does that help?[/color]
        >
        > Most probably not. This is not valid C and shouldn't compile.
        >[/color]
        Which one ?

        Comment

        • Suman

          #5
          Re: String appending



          Suman wrote:[color=blue]
          > Jirka Klaue wrote:[color=green]
          > > Suman:[color=darkred]
          > > > Selvesteen:[/color]
          > > ...[color=darkred]
          > > >> for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
          > > >> result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...[/color]
          > > ...[color=darkred]
          > > > /*--BEGIN--*/
          > > >
          > > > #include <stdio.h>
          > > >
          > > > #define PASTE(T1,T2) T1##T2
          > > > #define Append(N) (N, PASTE("SSL_",N) )
          > > >
          > > > int main()
          > > > {
          > > > printf("%s\n", Append("CLASS") );
          > > > return 0;
          > > > }
          > > > /*--END--*/
          > > >
          > > > Does that help?[/color]
          > >
          > > Most probably not. This is not valid C and shouldn't compile.
          > >[/color]
          > Which one ?[/color]
          Ah! ha. Hubris! Got it.

          Comment

          • Jirka Klaue

            #6
            Re: String appending

            Suman:[color=blue]
            > Jirka Klaue:[color=green]
            >> Suman:[/color][/color]
            ....[color=blue][color=green][color=darkred]
            >> > /*--BEGIN--*/
            >> >
            >> > #include <stdio.h>
            >> >
            >> > #define PASTE(T1,T2) T1##T2
            >> > #define Append(N) (N, PASTE("SSL_",N) )
            >> >
            >> > int main()
            >> > {
            >> > printf("%s\n", Append("CLASS") );
            >> > return 0;
            >> > }
            >> > /*--END--*/
            >> >
            >> > Does that help?[/color]
            >>
            >> Most probably not. This is not valid C and shouldn't compile.
            >>[/color]
            > Which one ?[/color]

            PASTE("A", "B") yields "A""B" which is not a valid preprocessing token.

            Do you use Microsoft's C-Compiler by any chance?

            Jirka

            Comment

            • john_bode@my-deja.com

              #7
              Re: String appending



              Selvesteen wrote:[color=blue]
              > Hello Gurus,
              >
              > I need to append a sub-string at the start of strings using C. What is
              > the best way to achieve that?
              >
              > for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
              > result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...
              >
              > Advance thanks for all your help.[/color]

              Slightly different approach from using macros:

              #include <stdio.h>

              #define MAX_NAME_LEN 10 /* length of longest result */
              /* string, not counting */
              /* terminator */
              #define MAX_NAME_COUNT 3

              int main(void)
              {
              char *prefix="SSL";
              char *names[MAX_NAME_COUNT]={"CLASS", "SEED", "RANDOM"};
              char results[MAX_NAME_COUNT][MAX_RESULT_LEN+ 1];
              int i;

              for (i = 0; i < MAX_NAME_COUNT; i++)
              {
              sprintf(results[i], "%s_%s", prefix, names[i]);
              printf("%s\n", results[i]);
              }

              return 0;
              }

              Comment

              • Suman

                #8
                Re: String appending



                Jirka Klaue wrote:[color=blue]
                > Suman:[color=green]
                > > Jirka Klaue:[color=darkred]
                > >> Suman:[/color][/color]
                > ...[/color]
                [snipped][color=blue]
                >
                > PASTE("A", "B") yields "A""B" which is not a valid preprocessing token.
                >
                > Do you use Microsoft's C-Compiler by any chance?[/color]
                Not if my manager's not around :)
                But this time, you've hit bull's eye! I did, and ... I apologise for
                causing (any/all) confusion for that code.

                I checked with gcc and it bit me hard.

                Comment

                • Randy Howard

                  #9
                  Re: String appending

                  On Fri, 1 Jul 2005 08:46:24 -0500, john_bode@my-deja.com wrote
                  (in article
                  <1120225583.988 367.53140@z14g2 000cwz.googlegr oups.com>):
                  [color=blue]
                  > Slightly different approach from using macros:
                  >
                  > #include <stdio.h>
                  >
                  > #define MAX_NAME_LEN 10 /* length of longest result */
                  > /* string, not counting */
                  > /* terminator */
                  > #define MAX_NAME_COUNT 3
                  >
                  > int main(void)
                  > {
                  > char *prefix="SSL";
                  > char *names[MAX_NAME_COUNT]={"CLASS", "SEED", "RANDOM"};[/color]

                  Delete this line:[color=blue]
                  > char results[MAX_NAME_COUNT][MAX_RESULT_LEN+ 1];[/color]
                  [color=blue]
                  > int i;
                  >
                  > for (i = 0; i < MAX_NAME_COUNT; i++)
                  > {[/color]
                  Delete this line:[color=blue]
                  > sprintf(results[i], "%s_%s", prefix, names[i]);[/color]

                  Delete this line:[color=blue]
                  > printf("%s\n", results[i]);[/color]

                  Add this line:
                  printf("%s_%s\n ", prefix, names[i]);[color=blue]
                  > }
                  >
                  > return 0;
                  > }
                  >[/color]



                  --
                  Randy Howard (2reply remove FOOBAR)

                  Comment

                  • john_bode@my-deja.com

                    #10
                    Re: String appending



                    Randy Howard wrote:[color=blue]
                    > On Fri, 1 Jul 2005 08:46:24 -0500, john_bode@my-deja.com wrote
                    > (in article
                    > <1120225583.988 367.53140@z14g2 000cwz.googlegr oups.com>):
                    >[color=green]
                    > > Slightly different approach from using macros:
                    > >
                    > > #include <stdio.h>
                    > >
                    > > #define MAX_NAME_LEN 10 /* length of longest result */
                    > > /* string, not counting */
                    > > /* terminator */
                    > > #define MAX_NAME_COUNT 3
                    > >
                    > > int main(void)
                    > > {
                    > > char *prefix="SSL";
                    > > char *names[MAX_NAME_COUNT]={"CLASS", "SEED", "RANDOM"};[/color]
                    >
                    > Delete this line:[color=green]
                    > > char results[MAX_NAME_COUNT][MAX_RESULT_LEN+ 1];[/color][/color]

                    [Remaining helpful hints snipped]

                    I thought it *might* be useful to the OP to demonstrate how to keep the
                    result strings around and save a few cycles later on.

                    Obviously, I was wrong. In the future I will endeavor to never suggest
                    anything so old-fashioned and stupid as keeping the results of your
                    operations around for later use.

                    Comment

                    Working...