Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- void JoinStrings(char* ppszSourceStringArray[],
- int nSourceStringArrayLength, char** ppszOutput,
- int *pnOutputLength) {
- if (ppszSourceStringArray == NULL) {
- return;
- }
- if (nSourceStringArrayLength <= 0) {
- return;
- }
- if (ppszOutput == NULL) {
- return;
- }
- if (pnOutputLength == NULL) {
- return;
- }
- int nTotalBytes = 0;
- for(int i = 0;i < nSourceStringArrayLength;i++) {
- const int CURRENT_ENTRY_SIZE
- = strlen(ppszSourceStringArray[i]) + 1;
- nTotalBytes += CURRENT_ENTRY_SIZE;
- *ppszOutput = (char*)realloc(*ppszOutput,
- (nTotalBytes)*sizeof(char));
- if (i == 0) {
- memset(*ppszOutput, 0, nTotalBytes);
- }
- strcat(*ppszOutput, ppszSourceStringArray[i]);
- }
- const int FINISHED_STRING_SIZE = strlen(*ppszOutput) + 1;
- *ppszOutput = (char*)realloc(*ppszOutput,
- (FINISHED_STRING_SIZE)*sizeof(char));
- (*ppszOutput)[FINISHED_STRING_SIZE - 1] = '\0';
- *pnOutputLength = FINISHED_STRING_SIZE;
- }
- int main() {
- const int NUM_STRINGS = 6;
- char* strings[] = {
- "Now is the time for all good men ",
- "to come to the aid of ",
- "their country.\nAnd to the Republic ",
- "for which it stands, ",
- "one nation, under God, indivisible, with liberty ",
- "and justice for all.\n"
- };
- int nOutputLength = 0;
- char *pszOutput = NULL;
- JoinStrings(strings, NUM_STRINGS, &pszOutput, &nOutputLength);
- printf(pszOutput);
- free(pszOutput);
- pszOutput = NULL;
- memset(strings, 0, NUM_STRINGS);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement