Thread: concatenating characters not character strings?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    concatenating characters not character strings?

    I have an array of characters (not a char string) and I would like to take certain elements

    example a[4] and a[13]

    and concatenate them together to use in an equality test such as ( == "JR")

    What is the simplest method to concatenate characters into a string to do this?

    strcat seems to work on strings not simple characters
    Last edited by ridgerunnersjw; 01-10-2021 at 03:36 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The easiest may well be to just compare the characters:
    Code:
    if (a[4] == 'J' && a[13] == 'R')
    If you really want to form a string out of them:
    Code:
    char sample[3] = "";
    sample[0] = a[4];
    sample[1] = a[13];
    if (strcmp(sample, "JR") == 0)
    Note that your '== "JR"' idea won't work because you'll end up comparing pointers rather than strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Thank you....
    strcmp was just what I was looking for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenating (adding) two character strings
    By Sergei in forum C Programming
    Replies: 1
    Last Post: 12-01-2015, 06:41 AM
  2. Concatenating strings
    By Junky in forum C Programming
    Replies: 2
    Last Post: 11-29-2009, 10:18 AM
  3. concatenating strings?
    By liri in forum C Programming
    Replies: 11
    Last Post: 08-24-2007, 05:34 PM
  4. Concatenating 2 strings
    By AngKar in forum C Programming
    Replies: 14
    Last Post: 04-25-2006, 10:51 AM
  5. Concatenating characters
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 08-22-2003, 09:48 PM

Tags for this Thread