Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

C String

by suresh

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];

  1. full_name is the string name
  2. 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

C String 1

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

C String 2

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

C String 3

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 4

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

Placed Under: C Programming

  • C Comments
  • C Escape Sequence
  • C Programming Operators
  • C Arithmetic Operators
  • C Assignment Operators
  • C Bitwise Operators
  • C Conditional Operator
  • C Increment & Decrement Optr
  • C Logical Operators
  • C Relational Operators
  • C Sizeof Operator
  • C If Statement
  • C IF ELSE Statement
  • C Else If Statement
  • C Nested If
  • C Break Statement
  • C Continue Statement
  • C Goto Statement
  • C Switch Case
  • C For Loop
  • C While Loop
  • C Do While Loop
  • C Arrays
  • C Two Dimensional Array
  • C Multi Dimensional Array
  • C String
  • C Structures
  • C Nested Structures
  • C Array of Structures
  • C Structures and Functions
  • C Union
  • C Structure & Union differences
  • C Pointers
  • C Pass Pointers to Functions
  • C GETS
  • C fgets Function
  • C fgetc
  • C fputs function
  • C fputc
  • C Functions
  • C Types of Functions
  • C Pass Array to Functions
  • Call By Value & Call By Reference
  • C Recursion
  • C Program Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy