I'm trying to concatenate characters in C, but no success. The problem is to take a string, check if there is space in that string, and create a new string from the letters that come after the space in that main string.
Example:
Main string: hello world wide
New string: hww
I have no idea how to concatenate. I researched on the internet, I saw that the strcpy
and strcat
functions can be useful, but even using them I am not successful. In the same way, I tried to do something like result += string[i + 1]
and it doesn't work.
Source code
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "str ing placeholder";
int stringLength = strlen(string);
int i;
char result;
for (i = 0; i < stringLength; i++)
{
if (string[i] == ' ')
{
printf("Found space at the index: %d\n", i);
result = string[i + 1];
printf("\nNext char: %c\n", result);
}
}
return 0;
}
Hope someone can guide me. I don't think my program logic is wrong, all I need is to take the first character of the string and each character that follows the space of a string and concatenate into a new string, then present this newly formed string.
chqrlie :
You cannot concatenate strings or characters with the + or += operators in C. You must define an array of char large enough to receive the new string and store the appropriate characters into it one at a time.\nYou probably want to copy the initial of each word to a buffer instead of every character that follows a space.\nHere is a modified version:\n#include <stdio.h>\n\nint main() {\n const char text[] = "Hello wild world!";\n char initials[sizeof text]; // array for the new string\n int i, j;\n char last = ' '; // pretend the beginning of the string follows a space\n\n // iterate over the main string, stopping at the null terminator\n for (i = j = 0; text[i] != '\\0'; i++) {\n // if the character is not a space but follows one\n if (text[i] != ' ' && last == ' ') {\n // append the initial to the new string\n initials[j++] = text[i];\n }\n last = text[i]; // update the last character\n }\n initials[j] = '\\0'; // set the null terminator in the new string.\n printf("%s\\n", initials); // output will be Hww\n return 0;\n}\n",
2021-12-06T20:10:42
Casotti :
If you want to concatenate the result in a string, 'result' should be a char array:\n//...\n\nchar result[4];\nint currentWord = 0;\n\nfor (i = 0; i < stringLength; i++)\n{\n if (string[i] == ' ')\n {\n result[currentWord] = string[i + 1];\n currentWord++;\n }\n}\n\nAnother problem with your code is that it wont read the first word, because it does not have a space before. One way to fix this is to assign the first of the string to the first element of the word:\nchar result[4];\nif (string[0] != ' ') result[0] = string[0];\nint currentWord = 1;\n\nYou can also use 'strtok_r' to simplify things, one way to implement it is like this:\nchar *saveptr;\nresult[0] = strtok_r(string, " ", &saveptr)[0];\nfor (i = 1; i < 3; i++) // 3 is the word count\n{\n result[i] = strtok_r(NULL, " ", &saveptr)[0];\n}\n\nNote that the size of the 'result' array is arbitrary and will only work with 3 or less words. You can create a similar for loop to count the number of spaces in the string to find out how many words there are.",
2021-12-06T19:27:35