C String

The one-dimensional array of characters followed by a null character \0 is called a string in C Programming. Let us see how to declare a string array, access elements or characters, and print characters with examples.

The syntax of a string declaration in C Programming is as follows. For Example, char full_name[50];. Here, full_name is the name, and the size equals 50. It means this allows a maximum of 49 characters.

char Name [Size];
  • Name: Please specify the name. For example, full_name, employee_name, etc
  • Size: Number of characters required for this plus one (\0). For instance, if Size =10, it can hold 9 characters.

C String Initialization

There are multiple ways to initialize a string.

char name[] = “Tutorial Gateway”; // Declare without Size

char name[50] = “Tutorial Gateway”; // Declare with Size

Declare string Characters Array in this programming language.

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 it using pointers

char *str;
char *name = “hello world”;

C Program to Declare and Print Strings

In this program, We are declaring the character array in possible ways. Next, we use the printf statement to print the char array. I suggest you refer to the Array article.

You must use %s to display the sentence or text as output. Or you can use the C Programming puts function.

#include  <stdio.h>

int main(int argc, const char * argv[]) {
    // Without Size
    char name1[] = "Tutorial Gateway";
    
    // With Size
    char name2[50] = "Tutorial Gateway";
    
    // Declare Characters Array
    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;
}
Declare and Print String in C programming

Allow Users to enter Text from the Command line

In this program, we are allowing users to enter their own string. Next, we print that user’s given sentences as output.

#include  <stdio.h>

int main(int argc, const char * argv[])
{
    char name1[50];
    printf("Please enter the Name : ");
    scanf("%s", name1);
    
    printf("Name: %s \n", name1);
    return 0;
}
Please enter the Name : TutorialGateway
Name: TutorialGateway 

Access Elements of Strings in C programming

You can use indexes to access individual letters. By this index, you can insert, delete, or update any string character at any given position.

#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;
}
Access Elements of a String Example

The analysis of the String or character array iteration-wise is 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 letter along with the index position.
Next, i value will increment

Second Iteration: while (name[1] != ‘\0’)
while (e != ‘\0’) – Condition True

C string Third Iteration: while (name[2] != ‘\0’)
while (l != ‘\0’) – It means the condition was True.

Fourth Iteration: while (name[3] != ‘\0’)
while (l != ‘\0’) – Condition True

Fifth Iter: while (name[4] != ‘\0’)
while (0 != ‘\0’) – This condition is True

Sixth Iteration:while (name[5] != ‘\0’)
while (\0 != ‘\0’) – Condition is False. So, the Compiler will exit from the While loop

How to find the String length?

In this program, we are using the built-in function strlen to find the length of a char array.

#include  <stdio.h>
#include  <string.h>

int main(int argc, const char * argv[])
{
    char nam[50];
    
    printf("Enter the Name : ");
    scanf("%s", nam);
    
    float len;
    len = strlen(name);
    printf("The Length = %.f \n", len);

    return 0;
}
Finding the String Length

C Programming language String Functions

The following is the list of available string functions in this language.

  1. memchr: Find the first occurrence of a character and return a pointer to it.
  2. strcat: To concat or combine
  3. strncat: This is the same as above. However, you can restrict the characters to add. It appends user-specified characters to the end.
  4. strcmp: Used to compare two and check whether they are equal or not.
  5. strncmp: This function is the same as strcmp. However, you can restrict the total number of characters to compare.
  6. strcpy: Used to shallow copy.
  7. strncpy: This is the same as strcpy. However, you can restrict the number of characters to copy.
  8. strcoll: Using LC_COLLATE settings, it will compare the two.
  9. strlen: Finds the total characters or length of it.
  10. strlwr: Converts to lowercase.
  11. strpbrk: It finds the first character in the first one that matches any character in the second one.
  12. strrev: Use this to reverse.
  13. strupr: Converts it to uppercase.
  14. strtok: It helps to tokenize the string using the given delimiter.
  15. strrchr
  16. strchr
  17. strstr