string array input in c
Language: C - String Array Input in C
To take input for a string array in C, you can follow the steps below:
- Declare the String Array:
- Start by declaring the string array with a fixed size or using dynamic memory allocation.
Example:
c char strings[5][20]; // Declaring a string array of 5 strings, each with a maximum length of 20 characters
Take Input for Each String:
- Use a loop to take input for each string in the array.
Example:
c for (int i = 0; i < 5; i++) { printf("Enter string %d: ", i+1); scanf("%s", strings[i]); }
Accessing the Input Strings:
- After taking input, you can access the strings in the array for further processing or display.
Example:
c for (int i = 0; i < 5; i++) { printf("String %d: %s\n", i+1, strings[i]); }
Complete Example:
Here's a complete example demonstrating the input and access of a string array in C: ```c #include
int main() { char strings[5][20]; for (int i = 0; i < 5; i++) { printf("Enter string %d: ", i+1); scanf("%s", strings[i]); } for (int i = 0; i < 5; i++) { printf("String %d: %s\n", i+1, strings[i]); } return 0; } ```
Remember to adjust the array size and string length according to your specific requirements.