Strings in C

A string in C is a sequence of characters stored in a character array and terminated with the special character '\0' called the null terminator. Unlike other languages, C does not have a built-in string datatypeβ€”strings are handled using arrays of char.

What is a String?

A string is a group of characters ending with a null character '\0'. This null terminator tells the compiler where the string ends.

Example – Simple String

#include <stdio.h>

int main() {

    char name[] = "Sourav";

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

    return 0;
}

How Strings Are Stored in Memory

Each character uses 1 byte. After the last character, C adds '\0' automatically.

char s[] = "CAT";

/* Memory Layout:
C | A | T | \0
*/

Example – Display Characters Individually

#include <stdio.h>

int main() {

    char s[] = "CAT";

    for(int i = 0; i < 4; i++){
        printf("%c\n", s[i]);
    }

    return 0;
}

Ways to Declare Strings

1. Declaring Without Initialization

char name[20];

2. Declaring With Initialization

char name[20] = "Sourav";

3. Character-by-Character

char word[4] = {'C','A','T','\0'};

Example – Taking Input into a String

#include <stdio.h>

int main() {

    char country[30];

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

    printf("Country: %s", country);

    return 0;
}

String Input Methods

Example – fgets() Input

#include <stdio.h>

int main() {

    char sentence[50];

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);

    printf("You wrote: %s", sentence);

    return 0;
}

Important String Functions (string.h)

⚠ Some functions like strupr(), strlwr(), strrev() are not standard C functions, but supported in many compilers (Turbo C/C++, GCC on Windows etc.).

Example – Using strlen()

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

int main() {

    char city[] = "Bhubaneswar";

    printf("Length = %lu", strlen(city));

    return 0;
}

Example – Using strcpy()

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

int main() {

    char a[] = "Hello";
    char b[20];

    strcpy(b, a);

    printf("Copied: %s", b);

    return 0;
}

Example – Using strcmp()

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

int main() {

    char a[] = "Apple";
    char b[] = "Mango";

    if(strcmp(a, b) == 0)
        printf("Same");
    else
        printf("Different");

    return 0;
}

String Handling Rules

Example – Uppercase Using Loop

#include <stdio.h>

int main() {

    char s[] = "sourav";

    for(int i = 0; s[i] != '\0'; i++){
        if(s[i] >= 'a' && s[i] <= 'z')
            s[i] -= 32;
    }

    printf("Uppercase: %s", s);

    return 0;
}

Real Life Uses of Strings

Practice Questions

  1. What is a string? How does C terminate a string?
  2. Explain memory layout of the string β€œHELLO”.
  3. Difference between scanf("%s") and fgets().
  4. List 5 string functions with small descriptions.
  5. Why is gets() unsafe?

Practice Task

Write a program that takes a full name and prints: βœ” Total characters βœ” Vowels count βœ” Consonants count βœ” Reversed string βœ” Name in Title Case