How to play with strings in C

Donotalo
706 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content