How to concatenate two charcters in c? - c

I am new to c .
When i try to do
char a = 'h';
Char b = 'j';
Strcat(a,b);
I get an error because a and b must be two strings .
So how can i do this ?

For the sake of completion here's a list of possible solutions:
gsamaras (esiest for your case):
char str[] = {a, b, '\0'};
Through an array:
char* string = (char*)malloc(3);
string[0] = 'a';
string[1] = 'b';
string[2] = '\0';
Generically:
char* str1 = "a";
char* str2 = "b";
size_t str1_len = 2;
size_t str2_len = 2;
char* new_str = (char*)malloc(str1_len - 1 + str2_len);
memcpy(new_str, str1, str1_len - 1);
memcpy(new_str + str1_len, str2, str2_len);

strcat() is a method used to concatenate strings. In your cases you have characters, so you shouldn't use this method.
Instead, you can create the string yourself, like this for example:
char str[] = {a, b, '\0'};

Take these two characters as string and then use strcat() function.
char a[] = "h";
char b[] = "j";
strcat(a,b);
printf("%s",a);

Related

Ending hex-specified section of a C string literal

char *a = "A\x01B";
I typed this and I meant A + \x01 + B, but the compiler thought that I meant A + \x1B. I was thinking it parses two characters after the \x as a hexadecimal value, but appears it is not. Then I thought maybe it parses three of them and typed:
char *a = "A\x001B";
But the result was the same, in fact even
char *a = "A\x000000000001B";
still means A+ 0x1b
So how do I get my next character, which is B parsed into a string literal as a character after the \x1?
how do I get my next character, which is B parsed into a string literal as a character after the \x1?
You can do:
const char *a = "A\x01" "B";
const char *a = "A\x1""B";
const char *a = "A\001B";
const char *a = "A\01B";
const char *a = "A\1B";
With slightly different meaning:
const char *a = (const char[]){'A', 1, 'B', 0};
const char a[] = {'A', 1, 'B', 0};

Concatenate two characters in c

How to concatenate two characters and store the result in a variable?
For example, I have 3 characters 'a', 'b' and 'c' and I need to join them together and store them in an array and later use this array as a string:
#include <stdio.h>
#include <string.h>
char n[10];
main ()
{
// now I want insert them in an array
//ex: n = { a + b + c }
}
There are many ways, the following 2 methods have exactly the same results: (using your code as a starting point.)
#include <stdio.h>
#include <string.h>
int main(void)
{
//Given the following
char a = 'a';
char b = 'b';
char c = 'c';
// assignment by initializer list at time of creation
char n1[10] = {a,b,c};
//usage of a string function
char n2[10] = {0}; //initialize array
sprintf(n2, "%c%c%c", a, b, c);
return 0;
}
Both result in a null terminated char arrays, or C strings.
Simply:
char a = 'a', b = 'b', c = 'c';
char str[4];
str[0] = a;
str[1] = b;
str[2] = c;
str[3] = '\0';
Or, if you want str to be stored on the heap (e.g. if you plan on returning it from a function):
char *str = malloc(4);
str[0] = a;
...
Any introductory book on C should cover this.
An assignment similar to that can only be done when the char array is declared using array initialiation with a brace-enclosed list:
char a = 'a', b = 'b', c = 'c';
char n[10] = {a, b, c};
After the declaration you can't do it like this because a char array is not a modifiable lvalue:
n = {a, b, c}; //error
To insert characters in an array that has been previously initialized, you need to either insert them one by one as exemplified in another answer, or use some library function like sprintf.
sprintf(n, "%c%c%c", a, b, c);
In both of my examples the char array will be null terminated by the compiler so you can use it as a string, if you assign the characters one by one, make sure to place a null terminator at the end ('\0'), only then will you have a propper string.

How do get individual words from a char array in C?

How do I convert something like:
const char phrase[] = "this is my phrase";
into something like:
char a[0] = "this";
char a[1] = "is";
char a[2] = "my";
char a[3] = "phrase";
Can I use pointers to access the words rather than the characters in a char array?

How to get character index of string in C?

How to get the current character index (in C) ?
char *s = "abcdefghijklmopqrstuvwxyz";
*s++;
*s++;
*s++;
printf("%c\n", *s); // print character 'd'
printf("%d\n", s - *s); // should print 3, but not working
I expect to get index (3), but how to code it programatically ?
char *s = "abcdefghijklmopqrstuvwxyz";
char *t = s;
*s++;
*s++;
*s++;
printf("%c\n", *s); // print character 'd'
printf("%d\n", s - t); // print 3
should do it.
You'll need to move another pointer (not the s pointer), to the third index, or any index for that matter. Then you can do pointer subtraction where the difference is the number of byte-elements between the pointers.
const char s[] = "asdf";
const char *s2 = s + 2;
printf( "%d", s2 - s ); // 2

string initialization in C

I am writing insertion sort using strings. I have char arrays like:
char array1[4] = {'a', 'b', 'c', '\0'};
char array2[4] = {'b', 'd', 'e', '\0'};
And I need to use this operation:
char string[2];
string[1] = array1;
string[2] = array2;
Is it possible ?
Because in insertion sort I need a string.
This is the insertion code:
char* insertionsort(char* a, int n) {
int k;
for (k = 1; k < n; ++k) {
int key = a[k];
int i = k - 1;
while ((i >= 0) && (key < a[i])) {
a[i + 1] = a[i];
--i;
}
a[i + 1] = key;
}
return a;
}
So I need to write this operation :
char string [2];
string[1] = array1;
string[2] = array2;
Is it possible ?
Short answer: no, it's not. Arrays are not assignable. You can use strcpy to copy the elements from one string to another, but in this case that won't work either -- you've defined string as an array of 2 chars, but you're trying to assign an entire array of chars to each of those.
It's not entirely clear that it's what you want, but one possibility would be:
char *string[2];
string[1] = array1;
string[2] = array2;
In this case, string is an array of two pointers to char, and the assignments set those to point to the first character of each of your previously defined strings.
Since you've tagged this as both C and C++, I'll add a bit about that: this is written as very much C code. If you're actually using C++, you probably want something more like:
std::string[2] = {"abc", "bde"};
Even in C, I'd prefer to initialize and use string constants where they make sense:
char array1[] = "abc";
char array2[] = "bde";
char *string[] = {array1, array2};
Or even just:
char const *string[] = {"abc", "bde"};
char array1[4] = {'a', 'b', 'c', '\0'}; // same as char *array1 = "abc";
char array2[4] = {'b', 'd', 'e', '\0'}; // same as char *array2 = "bde";
char *string [2]; // an array of 'pointers to chars' / array of strings
string[1] = array1; // this will work
string[2] = array2; // this will work
Of course not. Just remember,
char ary[4] = {'a', 'b', 'c', '\0'}; //define an array of char
When you use ary like this:
char string[2];
string[0] = ary;
ary degenerates from the name of an array to a pointer! So, what you do above is assigning a pointer to a char type, and which will lead to a type warning when compiling.
This is very important in C!
And you can fix it like this:
char *string[2]; //an array of pointers to char
string[1] = array1; //correct
string[2] = array2;

Resources