How do get individual words from a char array in C? - 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?

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};

Unsigned Short Array to Char Array

I've seen a little bit of code that deals with unsigned short arrays and storing "strings" in them. But I am curious how one might go about converting or extracting the string out of the unsigned short array into a char array for use in printf() for debugging purposes.
unsigned short a[5];
char b[5];
a[0] = 'h';
a[1] = 'e';
a[2] = 'l';
a[3] = 'l';
a[4] = 'o';
printf("%s\n", a);
// output is just the 'h'
From my recent understanding, an unsigned short is 2 bytes so the 'h' has a Null terminator with it. Whereas typically a char is used and each character is 1 byte.
Is there a good way to extract the "string" 'hello' and put it into a char[] so it can be printed out later? Iterate over the unsigned short array and store the value into a char array?
How to convert?:
char b[6];
for(size_t i = 0; i < 5; i++) b[i] = a[i];
b[5] = 0;

How to concatenate two charcters in 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);

Creating an int value from multiple char array fields in C

I'm working on a C program and i am struggling with it (I've been spoiled by the concept of object orientation).
What i want to do is this:
I want to put values in a char array into an int. So for example i have char[0] == '1' and char[1] == '2'. I want to put these values in an int variable so its value is 12. I have tried looking but I am not sure how to get this done.
I really am poor at explaining so please ask for more info if necessary.
If your char array is made with characters '1' and '2':
char a[2];
a[0] = '1';
a[1] = '2';
int b = (a[0]-'0')*10 + (a[1]-'0');
If your char array is made with numbers 1 and 2:
char a[2];
a[0] = 1;
a[1] = 2;
int b = a[0] * 10 + a[1];
also, see: Why does subtracting '0' in C result in the number that the char is representing?
If the character array contains a string that is if it is zero-terminated then you can apply standard C function atoi declared in header <stdlib.h>.
For example
char s[] = "12";
int x = atoi( s );
If the array is not zero-terminated as
char s[2] = "12";
then you can convert its content to an integer manually.
For example
int x = 0;
for ( size_t i = 0; i < sizeof( s ) / sizeof( *s ); i++ )
{
x = 10 * x + s[i] - '0';
}
What you are trying to do is called parsing. In c this can be done with the atoi() function like this:
char s[] = "12";
int num = atoi(s);

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