strlen() ! that is a good one.
I have another program
Code:
//an array of x cities whereby each city of y chars
int x = 3;
int y = 40;
char cities[x][y];
where I use gets() to read some strings with white spaces if available instead of scanf() and sscanf().
if I want to print the cities I'd get those weird characters so I have:
Code:
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
cities[i][j] = '\0';
}
}
or use strlen() after gets() to make '\0' char.
Code:
for (i = 0; i < x; i++) {
printf("Set city %d: ", i+1);
gets(cities[i]);
int len = strlen(cities[i]);
cities[i][len+1] = '\0';
}
that doesn't work !
the source code:
Code:
#include <stdio.h>
#include <string.h>
int main (void) {
//an array of x cities whereby each city of y chars
int x = 3;
int y = 40;
char cities[x][y];
int i, j; // loop counters
/* for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
cities[i][j] = '\0';
}
} */
for (i = 0; i < x; i++) {
printf("Set city %d: ", i+1);
gets(cities[i]);
int len = strlen(cities[i]);
cities[i][len+1] = '\0';
}
putchar('\n');
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
if (cities[i][j] != '\0') {
printf("[ %c ]", cities[i][j]);
}
}
putchar('\n');
}
return 0;
}