Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- char* smash(const char** words, size_t count) {
- size_t lengthStr = count;
- for (size_t i = 0; i < count; ++i) {
- lengthStr += strlen(words[i]);
- }
- char* newStr = malloc(lengthStr);
- char* cursor = newStr;
- for (int i = 0; i < count; ++i) {
- ptrdiff_t len = strlen(words[i]);
- memcpy(cursor, words[i], len);
- cursor += len;
- *cursor = ' ';
- cursor++;
- }
- *cursor = '\0';
- return newStr;
- }
- int main(int argc, char const *argv[]) {
- const char *words[] = {
- "hello", "world"
- };
- char* smashed = smash(words, sizeof(words) / sizeof(words[0]));
- printf("%s\n", smashed);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment