1.9 Strings in C Programming

Module 1.9 • Character Arrays, Core String Methods, Memory Layouts & String Parsing

1.9.1 Introduction to Strings

In C programming, a string is a collection of characters stored in a character array and terminated by a special character called the null character ('\0').

Unlike some programming languages, C does not provide a separate string data type. Strings are handled using character arrays.

Examples of Strings

"Computer"
"Programming"
"2026"
"Welcome to C"
""

Each string automatically ends with a null character.

For example:

char city[] = "Delhi";

Memory representation:

D e l h i \0

1.9.2 Declaring and Initializing Strings

Method 1: Using String Literal

char country[6] = "India";

Stored as:

I n d i a \0

Method 2: Using Character List

char city[] = {'P','u','n','e','\0'};

Both methods create the same string.

Method 3: Compiler Determines Size

char language[] = "Python";

Compiler automatically allocates enough space.

1.9.3 String and Character Difference

Character String
Enclosed in single quotesEnclosed in double quotes
Stores one characterStores multiple characters
Example: 'A'Example: "A"
char ch = 'A';
char str[] = "A";

These are different.

1.9.4 Displaying Strings

Strings are displayed using %s.

Example Program

#include <stdio.h>
 
int main()
{
    char name[] = "Karthik";
 
    printf("%s", name);
 
    return 0;
}

Output

Karthik

1.9.5 Reading Strings Using scanf()

Example Program

#include <stdio.h>
 
int main()
{
    char name[20];
 
    printf("Enter your name: ");
    scanf("%s", name);
 
    printf("Hello %s", name);
 
    return 0;
}

Sample Output

Enter your name: Ravi
Hello Ravi

Limitation

scanf("%s") stops reading at the first space.

Input:  Ravi Kumar
Stored: Ravi

Only the first word is read.

1.9.6 Reading Strings Using fgets()

Example Program

#include <stdio.h>
 
int main()
{
    char address[50];
 
    printf("Enter Address: ");
    fgets(address, sizeof(address), stdin);
 
    printf("Address: %s", address);
 
    return 0;
}

Output

Enter Address: Chennai Tamil Nadu
Address: Chennai Tamil Nadu

fgets() can read spaces.

1.9.7 Accessing Individual Characters

Strings are arrays of characters.

Example

#include <stdio.h>
 
int main()
{
    char word[] = "Coding";
 
    printf("%c\n", word[0]);
    printf("%c\n", word[5]);
 
    return 0;
}

Output

C
g

1.9.8 Printing Characters One by One

Example Program

#include <stdio.h>
 
int main()
{
    char text[] = "Learn";
    int i = 0;
 
    while(text[i] != '\0')
    {
        printf("%c\n", text[i]);
        i++;
    }
 
    return 0;
}

Output

L
e
a
r
n

1.9.9 String Length Using strlen()

The strlen() function returns the number of characters in a string.

Example Program

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str[] = "Engineer";
 
    printf("Length = %lu", strlen(str));
 
    return 0;
}

Output

Length = 8

The null character is not counted.

1.9.10 Copying Strings Using strcpy()

The strcpy() function copies one string into another.

Example Program

#include <stdio.h>
#include <string.h>
 
int main()
{
    char source[] = "Chennai";
    char destination[20];
 
    strcpy(destination, source);
 
    printf("%s", destination);
 
    return 0;
}

Output

Chennai

1.9.11 Concatenating Strings Using strcat()

Concatenation means joining two strings.

Example Program

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str1[30] = "Good";
    char str2[] = " Morning";
 
    strcat(str1, str2);
 
    printf("%s", str1);
 
    return 0;
}

Output

Good Morning

1.9.12 Comparing Strings Using strcmp()

String comparison cannot be done using ==. Use strcmp().

Example Program

#include <stdio.h>
#include <string.h>
 
int main()
{
    char s1[] = "Apple";
    char s2[] = "Apple";
 
    if(strcmp(s1, s2) == 0)
    {
        printf("Strings are Equal");
    }
    else
    {
        printf("Strings are Different");
    }
 
    return 0;
}

Output

Strings are Equal

1.9.13 Important String Functions

Function Purpose
strlen()Find length
strcpy()Copy string
strcat()Join strings
strcmp()Compare strings
strchr()Search character
strstr()Search substring

1.9.14 Reversing a String

Example Program

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str[] = "PROGRAM";
    int i;
 
    for(i = strlen(str)-1; i >= 0; i--)
    {
        printf("%c", str[i]);
    }
 
    return 0;
}

Output

MARGORP

1.9.15 Checking Palindrome String

A palindrome reads the same forwards and backwards.

Examples:

MADAM
LEVEL
RADAR

Example Program

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str[] = "LEVEL";
    int start = 0;
    int end = strlen(str) - 1;
    int flag = 1;
 
    while(start < end)
    {
        if(str[start] != str[end])
        {
            flag = 0;
            break;
        }
 
        start++;
        end--;
    }
 
    if(flag)
        printf("Palindrome");
    else
        printf("Not Palindrome");
 
    return 0;
}

Output

Palindrome

1.9.16 Converting Lowercase to Uppercase

Example Program

#include <stdio.h>
 
int main()
{
    char str[] = "welcome";
    int i = 0;
 
    while(str[i] != '\0')
    {
        if(str[i] >= 'a' && str[i] <= 'z')
        {
            str[i] = str[i] - 32;
        }
 
        i++;
    }
 
    printf("%s", str);
 
    return 0;
}

Output

WELCOME

1.9.17 Counting Vowels in a String

Example Program

#include <stdio.h>
 
int main()
{
    char str[] = "Education";
    int i, count = 0;
 
    for(i = 0; str[i] != '\0'; i++)
    {
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||
           str[i]=='o'||str[i]=='u'||
           str[i]=='A'||str[i]=='E'||str[i]=='I'||
           str[i]=='O'||str[i]=='U')
        {
            count++;
        }
    }
 
    printf("Vowels = %d", count);
 
    return 0;
}

Output

Vowels = 5

1.9.18 Array of Strings

Multiple strings can be stored in a two-dimensional character array.

Example

char fruits[4][15] =
{
    "Mango",
    "Orange",
    "Apple",
    "Grapes"
};

Displaying Array of Strings

#include <stdio.h>
 
int main()
{
    char fruits[4][15] =
    {
        "Mango",
        "Orange",
        "Apple",
        "Grapes"
    };
 
    int i;
 
    for(i=0;i<4;i++)
    {
        printf("%s\n", fruits[i]);
    }
 
    return 0;
}

Output

Mango
Orange
Apple
Grapes

1.9.19 Array of Pointers to Strings

Instead of a 2D array, pointers can store string addresses.

Example

char *months[] =
{
    "January",
    "February",
    "March",
    "April"
};

Access:

printf("%s", months[2]);

Output

March

This method often uses less memory.

1.9.20 Dynamic Memory Allocation for Strings

When string size is unknown at compile time:

Example Program

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char *str;
 
    str = (char *)malloc(30 * sizeof(char));
 
    if(str == NULL)
    {
        printf("Memory Allocation Failed");
        return 1;
    }
 
    printf("Enter Text: ");
    fgets(str, 30, stdin);
 
    printf("You Entered: %s", str);
 
    free(str);
 
    return 0;
}

1.9.21 Common Mistakes with Strings

Comparing Using ==

Wrong:

if(str1 == str2)

Correct:

if(strcmp(str1, str2) == 0)

Insufficient Memory

Wrong:

char name[4] = "Ramesh";

Not enough space.

Correct:

char name[7] = "Ramesh";

Forgetting Null Character

Every string must end with:

'\0'

1.9.22 Real-World Applications of Strings

Strings are used in:

Summary

Verify Comprehension: Technical Knowledge Assessment

Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.