Let us see how to declare C string array, access string elements, print elements of a string in c with examples — the one-dimensional array of characters followed by a null character \0 called as a string in C Programming.
C string Declaration or Syntax
The syntax of a string in C Programming is as follows:
char String_Name [String_Size];
- String_Name: Please specify the name of a string. For example full_name, employee_name, etc
- String_Size: Number of characters required for this string plus one (\0). For example, String_Size =10, then the string can hold 9 characters.
For Example,
char full_name[50];
- full_name is the string name
- The size of a string is 50. It means, this string allows a maximum of 49 characters
C String Initialization
There are multiple ways to initialize string C Programming
// Declare C String without Size char name[] = "Tutorial Gateway";
// Declare String with Size char name[50] = "Tutorial Gateway";
// Declare String of Characters char name[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 'G', 'a', 't', 'e', 'w', 'a', 'y', '\0'}; char name[16] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l',' G', 'a', 't', 'e', 'w', 'a', 'y', '\0'};
You can also declare a string using pointers
char *str; char *name = "hello world";
C Program to Declare and Print Strings
In this program, We are declaring the string in possible ways. Next, we are using the printf statement to print those strings. I suggest you refer Arrays in C article.
NOTE: You have to use %s to display the string as output. Or you use C Programming puts function
/* C String Example */ #include <stdio.h> int main(int argc, const char * argv[]) { // Declare C String without Size char name1[] = "Tutorial Gateway"; // Declare String with Size char name2[50] = "Tutorial Gateway"; // Declare String of Characters char name3[] = {'T','u','t','o','r','i','a','l','G','a','t','e','w','a','y', '\0'}; char name4[16] = {'T','u','t','o','r','i','a','l','G','a','t','e','w','a','y', '\0'}; printf("Name1: %s \n", name1); printf("Name2: %s \n", name2); printf("Name3: %s \n", name3); printf("Name4: %s \n", name4); return 0; }
OUTPUT
Allow Users to enter String from Command line
In this program, we are allowing users to enter their own string. Next, we are printing that user given string as output.
/* C String Example */ #include <stdio.h> int main(int argc, const char * argv[]) { char name[50]; printf("Please enter the Name : "); scanf("%s", name); printf("Name: %s \n", name); return 0; }
OUTPUT
Access Elements of a Strings in C programming
You can use indexes to access individual characters in a string. By this, you can insert, delete, or update any character at any given position.
/* C String Example */ #include <stdio.h> int main(int argc, const char * argv[]) { char name[50]; int i = 0; printf("Please enter the Name : "); scanf("%s", name); while (name[i] != '\0') { printf("The Character at %d Index Position = %c \n", i, name[i]); i++; } return 0; }
OUTPUT
ANALYSIS
The analysis of the C string array in iteration wise is as shown below
First Iteration : while (name[i] != ‘\0’)
Here, i value is 0. It means, name[0] = h So, condition is True
It will print that character along with the index position.
Next, i value will increment
Second Iteration: while (name[1] != ‘\0’)
while (e != ‘\0’) – Condition True
Third Iteration: while (name[2] != ‘\0’)
while (l != ‘\0’) – It means, Condition was True
Fourth Iteration: while (name[3] != ‘\0’)
while (l != ‘\0’) – Condition True
Fifth Iteration: while (name[4] != ‘\0’)
while (0 != ‘\0’) – This condition is True
Sixth Iteration:while (name[5] != ‘\0’)
while (\0 != ‘\0’) – Condition is False. So, Compiler will exit from While loop
String length in C Programming
In this program, we are using the built-in string function strlen to find the length of a given string
/* C String Example */ #include <stdio.h> #include <string.h> int main(int argc, const char * argv[]) { char name[50]; printf("Please enter the Name : "); scanf("%s", name); float len; len = strlen(name); printf("The Length of a Given String %s = %.f \n", name, len); return 0; }
OUTPUT
C String Functions
The following are the list of available string functions in C Programming
- memchr: Find the first occurrence of a character and returns a pointer to it.
- strcat: To concat or combine strings
- strncat: This is the same as above. However, you can restrict the characters to add. It appends user-specified characters to the end of a string.
- strcmp: Used to compare two string to check whether they are equal or not.
- strncmp: This C string function is the same as strcmp. However, you can restrict the total number of characters to compare.
- strcpy: Used to shallow copy the string
- strncpy: This is the same as strcpy. However, you can restrict the number of characters to copy.
- strcoll: Using LC_COLLATE settings, it will compare two string
- strlen: Finds the length of a string
- strlwr: Converts the string to lowercase
- strpbrk:It finds the first character in the first string that matches any character in a second-string
- strrev: Use this to reverse a given string.
- strupr: Converts the string to uppercase