Combining string and chararcter into string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dn6326
    New Member
    • Sep 2007
    • 6

    Combining string and chararcter into string

    hi again, basically i want to add a character c onto string s,

    so far ive tried s = strcat(s, c)
    but it says they are incompatable

    and i tried s = ("%s%c", s, c)

    but again no luck...

    any ideas?

    cheers
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dn6326
    hi again, basically i want to add a character c onto string s,

    so far ive tried s = strcat(s, c)
    but it says they are incompatable

    and i tried s = ("%s%c", s, c)

    but again no luck...

    any ideas?

    cheers
    Sure, just do it yourself: suppose you have a buffer large enough for the string
    and at least one more character:

    [code=c]
    char buffer[42]= "fo";
    [/code]

    then you can append that character c as follows:

    [code=c]
    size_t i= strlen(buffer); // i is the index where the '\0' char is stored
    buffer[i++]= c; // overwrite the '\0' char with c
    buffer[i]= '\0'; // and add a new terminating '\0' char
    [/code]

    Of course you should create a little function for that.

    kind regards,

    Jos

    Comment

    • syamas
      New Member
      • Sep 2007
      • 10

      #3
      Originally posted by dn6326
      hi again, basically i want to add a character c onto string s,

      so far ive tried s = strcat(s, c)
      but it says they are incompatable

      and i tried s = ("%s%c", s, c)

      but again no luck...

      any ideas?

      cheers

      do one thing

      try strcopy(s,"hell o");
      then append c onto string s
      ie strcat(s,"world "); or strcat(s,'c'); or strcat(s,"c");
      then s becomes "helloworld " okkkk

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by syamas
        do one thing

        try strcopy(s,"hell o");
        then append c onto string s
        ie strcat(s,"world "); or strcat(s,'c'); or strcat(s,"c");
        then s becomes "helloworld " okkkk
        More from :
        The OP wants to append a single char to that string; you can't use the strcat()
        function for that. That's what his/her problem was all about.

        kind regards,

        Js

        Comment

        Working...