Simple C question

This is a discussion on Simple C question within the C Programming forums, part of the General Programming Boards category; This should be rather easy, but I'm burnt out...How do I add a character to a C string (char array)? ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    Simple C question

    This should be rather easy, but I'm burnt out...How do I add a character to a C string (char array)? Ex:

    str = "programmer"

    and I want

    str = "programmers"

    strcat does not work, neither does simply using +s as in C++. Please help

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    why won't strcat work?

    Are you using it properly?

    Code:
    SYNOPSIS
           #include <string.h>
    
           char *strcat(char *dest, const char *src);
    ~/

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> strcat does not work

    the second argument to strcat is a character array, try:

    strcat(s, "s");

    or, just traverse the string yourself, append the char where the NULL was, increment, then add a NULL (just be sure the string has enough space allocated to it to accomadate the new data).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    the second argument to strcat is a character array, try:

    strcat(s, "s");


    Hey that is cool.

    ~/

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    Quote Originally Posted by Sebastiani
    >> strcat does not work

    the second argument to strcat is a character array, try:

    strcat(s, "s");

    or, just traverse the string yourself, append the char where the NULL was, increment, then add a NULL (just be sure the string has enough space allocated to it to accomadate the new data).
    NULL is a macro that should only be used in pointer context. The myriad meanings of null can potentially confuse people, so you should be more specific. In this case, "null character" would be more appropriate than any lexical variation of "null". NULL is the worst possible choice in my opinion because it means something completely different than what you were using it for.

    >>How do I add a character to a C string (char array)?
    Provided the array has enough room, strcat can be used as shown previously. Though if the character you wish to add is a variable, strcat will not work unless it too is a string. In the case where there is enough room and the character to be added is not a string, you will need to find the end of the array and perform the modification manually. Here is one way to go about it:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char str[12] = "Programmer";
      char ch = 's';
      size_t end;
    
      end = strlen(str);
      str[end] = ch;
      str[end + 1] = '\0';
      puts(str);
    
      return 0;
    }
    Notice that the expression end + 1 is used. If there is not enough room in the array, this would assign to memory outside of the array boundaries, resulting in undefined behavior. So you should be very careful when working directly with strings like this. To avoid the potential problems, you can write a function to do it for you. Then you have a fully tested and debugged routine to call:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void append(char *str, char ch);
    
    int main()
    {
      char str[12] = "Programmer";
      char ch = 's';
      
      append(str, ch);
      puts(str);
    
      return 0;
    }
    
    void append(char *str, char ch)
    {
      size_t end = strlen(str);
    
      str[end] = ch;
      str[end + 1] = '\0';
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> NULL is a macro that should only be used in pointer context.

    You are absolutely right of course - it was a poor choice of words.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>strcat does not work
    Why not post the code that you've tried, then everyone can stop guessing at what you've done wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suspect they're trying to concatenate two string literals.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21