C String

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];. Here, full_name is the string name and 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.

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;
}
Name1: Tutorial Gateway 
Name2: Tutorial Gateway 
Name3: TutorialGateway 
Name4: TutorialGateway 

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;
}
Please enter the Name : TutorialGateway
Name: TutorialGateway 

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;
}
C String 3

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;
}
Please enter the Name : tutorialgateway
The Length of a Given String tutorialgateway = 15 

C String Functions

The following are the list of available string functions in C Programming

  1. memchr: Find the first occurrence of a character and returns a pointer to it.
  2. strcat: To concat or combine strings
  3. 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.
  4. strcmp: Used to compare two string to check whether they are equal or not.
  5. strncmp: This C string function is the same as strcmp. However, you can restrict the total number of characters to compare.
  6. strcpy: Used to shallow copy the string
  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 two string
  9. strlen: Finds the length of a string
  10. strlwr: Converts the string to lowercase
  11. strpbrk:It finds the first character in the first string that matches any character in a second-string
  12. strrev: Use this to reverse a given string.
  13. strupr: Converts the string to uppercase