Back
Close

How to play with strings in C

Donotalo
49.5K views
Previous: String Split

If a C string is a one dimensional character array then what's an array of C string looks like? It's a two dimensional character array!

Here is how an array of C string can be initialized:

#define NUMBER_OF_STRING 4
#define MAX_STRING_SIZE 40

char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] =
{ "array of c string",
  "is fun to use",
  "make sure to properly",
  "tell the array size"
};

Since all but the highest dimension can be omitted from an array declaration, the above declaration can be reduced to:

#define MAX_STRING_SIZE 40

char arr[][MAX_STRING_SIZE] =
{ "array of c string",
  "is fun to use",
  "make sure to properly",
  "tell the array size"
};

But it is a good practice to specify size of both dimensions.

Now each arr[x] is a C string and each arr[x][y] is a character. You can use any function on arr[x] that works on string!

Following example prints all strings in a string array with their lengths.

#define NUMBER_OF_STRING 4
#define MAX_STRING_SIZE 40

char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] =
{ "array of c string",
  "is fun to use",
  "make sure to properly",
  "tell the array size"
};

for (int i = 0; i < NUMBER_OF_STRING; i++)
{
    printf("'%s' has length %d\n", arr[i], strlen(arr[i]));
}

Following example reverses all strings in a string array:

#include <stdio.h>
#include <string.h>
#define NUMBER_OF_STRING 4
#define MAX_STRING_SIZE 40
void print_array(const char arr[NUMBER_OF_STRING][MAX_STRING_SIZE])
{
for (int i = 0; i < NUMBER_OF_STRING; i++)
{
printf("'%s' has length %d\n", arr[i], strlen(arr[i]));
}
}
int main()
{
char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] =
{ "array of c string",
"is fun to use",
"make sure to properly",
"tell the array size"
};
printf("Before reverse:\n");
print_array(arr);
for (int i = 0; i < NUMBER_OF_STRING; i++)
{
for (int j = 0, k = strlen(arr[i]) - 1; j < k; j++, k--)
{
char temp = arr[i][j];
arr[i][j] = arr[i][k];
arr[i][k] = temp;
}
}
printf("\nAfter reverse:\n");
print_array(arr);
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Following example concatenates all strings in a string array with space between them into a single string:

#include <stdio.h>
#include <string.h>
#define NUMBER_OF_STRING 4
#define MAX_STRING_SIZE 40
#define DEST_SIZE 100
int main()
{
char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] =
{ "array of c string",
"is fun to use",
"make sure to properly",
"tell the array size"
};
char dest[DEST_SIZE] = "";
for (int i = 0; i < NUMBER_OF_STRING; i++)
{
strcat(dest, arr[i]);
if (i < NUMBER_OF_STRING - 1)
{
strcat(dest, " ");
}
}
printf(dest);
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants