Strings in C Programming (2026 Guide) – Syntax, Functions, Examples & Practice Programs for Beginners

Before learning Strings in C Programming, it is highly recommended to understand these important topics:

👉 Variables In C
👉 Functions In C
👉 Arrays In C
👉 Pointers In C

Why?

Because strings in C are closely connected with arrays and pointers.

Arrays store multiple values.
Characters are single letters like ‘A’, ‘B’, or ‘C’.
Strings are collections of characters stored together.

Important: In C programming, strings are actually character arrays ending with a special character called \0.

What is a String in C?

A string in C is a collection of characters stored inside a character array.

Example:

char name[] = "Hello";

Here:

  • H → character
  • e → character
  • l → character
  • l → character
  • o → character
  • \0 → null character (end of string)
Every string in C automatically ends with \0. This tells the compiler where the string ends.

Why Do We Need Strings in C?

In real-world programming, we often work with text data.

  • Student names
  • Passwords
  • Messages
  • Emails
  • Addresses
  • Search keywords

Strings help us:

  • ✔ Store text easily
  • ✔ Process characters efficiently
  • ✔ Build user-friendly applications
  • ✔ Handle input and output operations

Syntax of Strings in C

char string_name[size];

Example:

char name[20];
  • char → character data type
  • name → string variable name
  • 20 → maximum size of string

How to Initialize Strings

Method 1

char name[] = "Hello";

Method 2

char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Method 3

char name[20] = "Programming";

Reading Strings in C

Strings can be entered using:

  • ✔ scanf()
  • ✔ gets()
  • ✔ fgets()

Using scanf()

char name[20];

scanf("%s", name);
scanf() stops reading at space.

Using gets()

gets(name);
⚠ gets() is unsafe and outdated.

Using fgets() (Recommended)

fgets(name, sizeof(name), stdin);
Safer and recommended in modern C programming.

Displaying Strings

Using printf()

printf("%s", name);

Using puts()

puts(name);

String Functions in C

C provides many built-in string functions in string.h.

#include <string.h>

strlen() – Find Length of String

char name[] = "Hello";

printf("%d", strlen(name));
Output: 5

strcpy() – Copy String

char str1[] = "Hello";
char str2[20];

strcpy(str2, str1);

printf("%s", str2);

strcat() – Concatenate Strings

char str1[20] = "Hello ";
char str2[] = "World";

strcat(str1, str2);

printf("%s", str1);
Output: Hello World

strcmp() – Compare Strings

char str1[] = "Apple";
char str2[] = "Apple";

printf("%d", strcmp(str1, str2));
Output: 0 → strings are equal

strrev() – Reverse String

char str[] = "Hello";

strrev(str);

printf("%s", str);
Output: olleH

Accessing String Characters

char name[] = "Hello";

printf("%c", name[1]);
Output: e

Strings and Loops in C

char name[] = "Hello";
int i;

for(i = 0; name[i] != '\0'; i++)
{
    printf("%c\n", name[i]);
}

Strings and Pointers in C

char *ptr = "Hello";

printf("%s", ptr);

Two-Dimensional String Array

char names[3][20] = {
    "John",
    "Alice",
    "David"
};

Advantages of Strings in C

  • ✔ Easy text handling
  • ✔ Efficient memory usage
  • ✔ Useful for input/output
  • ✔ Supports many built-in functions
  • ✔ Widely used in real applications

Limitations of Strings in C

  • Fixed size arrays
  • Manual memory management
  • Risk of buffer overflow
  • String handling can become complex

Practice Programs on Strings

🟢 Easy Level:

  • Program to find length of string
  • Program to display string characters

🟡 Medium Level:

  • Program to copy string
  • Program to compare two strings

🔴 Advanced Level:

  • Program to reverse string
  • Program to check palindrome string
  • Program to count vowels and consonants

Real-World Uses of Strings

  • Login systems
  • Search engines
  • Chat applications
  • Databases
  • Web development
  • File handling
  • Text editors

Difference Between Character and String

Character String
Single letter Collection of characters
Uses single quotes Uses double quotes
Example: 'A' Example: "Apple"

Conclusion

✔ Store text data
✔ Process characters
✔ Build interactive programs
✔ Handle real-world applications

FAQ – Strings in C

Q1: What is a string in C?

A string is a collection of characters ending with \0.

Q2: Which data type is used for strings?

char array.

Q3: Which header file is used for string functions?

string.h

Q4: What does strlen() do?

It finds the length of a string.

Q5: Why is \0 important in strings?

It marks the end of the string.


Keep practicing and keep coding 💛

Related Posts:

Comments

Popular posts from this blog