Wednesday, 12 August 2026

Strings in C Programming

🔤 Strings in C Programming

Character Arrays, String Functions & Practical Examples

Learn How C Programs Store and Manipulate Text 🚀


🚀 Introduction

In the previous lesson, we learned about arrays. An array can store multiple values of the same data type.

But what if we want to store text such as:

  • Your name
  • Your city
  • A sentence
  • A password
  • A message

In C programming, text is generally stored using a character array, which we commonly call a string.

💡 Simple Definition

A string in C is a sequence of characters terminated by a special null character '\0'.


🔤 String as a Character Array

Consider the word Hello.

char name[] = "Hello";

Internally, C stores the characters followed by the null character:

Index 0 1 2 3 4 5
Character H e l l o \0

📝 Declaring a String

A string can be declared using a character array.

char name[20];

This creates space for up to 19 visible characters plus the terminating '\0'.

You can also initialize a string immediately:

char name[] = "Rahul";

🖥️ Printing a String

The %s format specifier is used to print a string.

#include <stdio.h>

int main()
{
    char name[] = "Rahul";

    printf("Name = %s", name);

    return 0;
}

Output:

Name = Rahul

⌨️ Taking String Input

A simple way to read a single word is using scanf() with %s.

#include <stdio.h>

int main()
{
    char name[50];

    printf("Enter your name: ");
    scanf("%49s", name);

    printf("Hello %s", name);

    return 0;
}

Notice that we do not use & before the array name in this example.

⚠️ Important: scanf("%s", ...) normally reads only up to whitespace. For a full sentence containing spaces, use fgets().

📖 Reading a Full Line with fgets()

If the user enters a sentence such as "I love computer programming", fgets() is a better choice.

#include <stdio.h>

int main()
{
    char message[100];

    printf("Enter a message: ");

    fgets(message, sizeof(message), stdin);

    printf("You entered: %s", message);

    return 0;
}

🛠️ Important String Functions

C provides several useful functions for working with strings through the <string.h> library.

Function Purpose Example
strlen() Finds string length strlen(name)
strcpy() Copies a string strcpy(a,b)
strcmp() Compares strings strcmp(a,b)
strcat() Joins strings strcat(a,b)

📏 strlen() – Find String Length

The strlen() function returns the number of characters in a string, excluding the terminating '\0'.

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

int main()
{
    char name[] = "Computer";

    printf("Length = %zu", strlen(name));

    return 0;
}

Output:

Length = 8

📋 strcpy() – Copy a String

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

int main()
{
    char source[] = "Programming";
    char destination[30];

    strcpy(destination, source);

    printf("%s", destination);

    return 0;
}

Here, the contents of source are copied into destination.


⚖️ strcmp() – Compare Two Strings

The strcmp() function compares two strings.

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

int main()
{
    char a[] = "Hello";
    char b[] = "Hello";

    if(strcmp(a, b) == 0)
    {
        printf("Strings are equal");
    }
    else
    {
        printf("Strings are different");
    }

    return 0;
}
💡 Remember: Do not normally compare C strings using ==. Use strcmp().

🔗 strcat() – Join Two Strings

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

int main()
{
    char first[50] = "Best";
    char second[] = "Programming";

    strcat(first, second);

    printf("%s", first);

    return 0;
}

Output:

BestProgramming

💻 Complete Practical Example

Let's create a small program that asks for a user's name and displays its length.

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

int main()
{
    char name[50];

    printf("Enter your name: ");

    fgets(name, sizeof(name), stdin);

    printf("\nYour name is: %s", name);

    printf("Number of characters: %zu", strlen(name));

    return 0;
}

⚠️ Common Beginner Mistakes

  • Forgetting to include <string.h> when needed.
  • Confusing a character with a string.
  • Forgetting the terminating null character when manually creating strings.
  • Using an array that is too small for the intended text.
  • Trying to compare strings using ==.

🔤 Character vs String

Character String
'A' "Apple"
Uses single quotes Uses double quotes
One character Multiple characters
char grade = 'A'; char name[] = "Apple";

🌍 Where Are Strings Used?

  • 👤 User names
  • 🔐 Passwords and authentication data
  • 📧 Email addresses
  • 💬 Messages and comments
  • 📄 Text processing
  • 🌐 Website and application data

🚀 Practice Programs

  1. Take the user's name and display it.
  2. Find the length of a string.
  3. Count the number of vowels in a string.
  4. Reverse a string.
  5. Compare two strings.
  6. Copy one string into another.
  7. Join two strings together.
  8. Check whether a word is a palindrome.

🧠 Quick Revision

  • String: Sequence of characters ending with '\0'.
  • %s: Used to print a string.
  • fgets(): Useful for reading a complete line.
  • strlen(): Finds string length.
  • strcpy(): Copies a string.
  • strcmp(): Compares two strings.
  • strcat(): Joins strings.

🎯 Conclusion

Strings are essential whenever a program needs to work with text. Understanding character arrays and standard string functions will make it much easier to build useful C programs.

You now know how to declare, input, display, compare, copy and join strings in C.

Next: Functions in C Programming 🧩💻

No comments:

Post a Comment