C Programming

Lecture 12: Strings | Unit III: Arrays and Functions

Instructor

Dr. Mohsin Dar
Assistant Professor
Cloud & Software Operations Cluster

Course Details

BTech First Semester
C Programming
UPES

Prerequisites

Arrays (1D)
2D Arrays
Basic C Syntax

Introduction to Strings

A string in C is a sequence of characters terminated by a null character ('\0'). Strings are essentially one-dimensional character arrays with special properties and handling mechanisms.

Key Characteristics:

  • Strings are arrays of characters
  • Always terminated by null character ('\0')
  • Stored in contiguous memory locations
  • No built-in string data type (unlike some other languages)
  • Length is determined by the null terminator
Connection to Arrays: Since you've learned about arrays and 2D arrays, think of strings as a specialized case of character arrays where the last element is always '\0'.

String Declaration and Initialization

Method 1: Character Array with Size

char str[10]; // Declares a character array of size 10

Method 2: Initialize with String Literal

char str[] = "Hello"; // Size automatically determined (6 chars including '\0') char str[20] = "Hello"; // Size 20, only first 6 positions used

Method 3: Character-by-Character Initialization

char str[] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Manual null termination

Complete Example:

#include <stdio.h> int main() { // Different ways to declare and initialize strings char str1[50]; // Declaration only char str2[] = "Programming"; // Auto-sized char str3[20] = "C Language"; // Fixed size char str4[] = {'U', 'P', 'E', 'S', '\0'}; // Character array printf("str2: %s\n", str2); printf("str3: %s\n", str3); printf("str4: %s\n", str4); return 0; }
str2: Programming str3: C Language str4: UPES

String Input and Output

Input Functions

Function Purpose Limitation
scanf("%s", str) Read single word Stops at whitespace
gets(str) Read entire line Unsafe (deprecated)
fgets(str, size, stdin) Safe line input May include newline

Output Functions

Function Purpose Usage
printf("%s", str) Print string Most common
puts(str) Print string + newline Automatic newline
fputs(str, stdout) Print without newline No automatic newline

String I/O Example:

#include <stdio.h> int main() { char name[50]; char sentence[100]; printf("Enter your name: "); scanf("%s", name); // Only first word printf("Enter a sentence: "); getchar(); // Clear input buffer fgets(sentence, 100, stdin); // Entire line printf("Name: %s\n", name); printf("Sentence: %s", sentence); return 0; }
Security Note: Never use gets() function as it can cause buffer overflow. Always use fgets() for safe string input.

String Library Functions

The <string.h> header provides several useful functions for string manipulation:

Length Functions

#include <string.h> int strlen(char *str); // Returns length of string (excluding '\0')

Copy Functions

char *strcpy(char *dest, char *src); // Copy src to dest char *strncpy(char *dest, char *src, int n); // Copy n characters

Concatenation Functions

char *strcat(char *dest, char *src); // Concatenate src to dest char *strncat(char *dest, char *src, int n); // Concatenate n characters

Comparison Functions

int strcmp(char *str1, char *str2); // Compare two strings int strncmp(char *str1, char *str2, int n); // Compare n characters

String Functions Example:

#include <stdio.h> #include <string.h> int main() { char str1[50] = "Hello"; char str2[50] = "World"; char str3[100]; // Length printf("Length of str1: %d\n", strlen(str1)); // Copy strcpy(str3, str1); printf("After copying: %s\n", str3); // Concatenation strcat(str1, " "); strcat(str1, str2); printf("After concatenation: %s\n", str1); // Comparison if (strcmp("ABC", "ABC") == 0) { printf("Strings are equal\n"); } return 0; }
Length of str1: 5 After copying: Hello After concatenation: Hello World Strings are equal

String Comparison Rules

The strcmp() function returns:

  • 0: If strings are equal
  • Positive value: If first string is lexicographically greater
  • Negative value: If first string is lexicographically smaller

String Comparison Example:

#include <stdio.h> #include <string.h> int main() { char *str1 = "Apple"; char *str2 = "Banana"; char *str3 = "Apple"; printf("strcmp(Apple, Banana): %d\n", strcmp(str1, str2)); // Negative printf("strcmp(Banana, Apple): %d\n", strcmp(str2, str1)); // Positive printf("strcmp(Apple, Apple): %d\n", strcmp(str1, str3)); // Zero return 0; }
strcmp(Apple, Banana): -1 strcmp(Banana, Apple): 1 strcmp(Apple, Apple): 0

Character-wise String Processing

Since strings are character arrays, you can process them using loops, just like the arrays you've learned about:

Manual String Length Calculation:

#include <stdio.h> int stringLength(char str[]) { int length = 0; while (str[length] != '\0') { length++; } return length; } int main() { char text[] = "Programming"; printf("String: %s\n", text); printf("Length: %d\n", stringLength(text)); // Print each character printf("Characters: "); for (int i = 0; text[i] != '\0'; i++) { printf("%c ", text[i]); } printf("\n"); return 0; }
String: Programming Length: 11 Characters: P r o g r a m m i n g

String Reversal:

#include <stdio.h> #include <string.h> void reverseString(char str[]) { int start = 0; int end = strlen(str) - 1; char temp; while (start < end) { // Swap characters temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } int main() { char text[] = "Hello World"; printf("Original: %s\n", text); reverseString(text); printf("Reversed: %s\n", text); return 0; }
Original: Hello World Reversed: dlroW olleH

Array of Strings (2D Character Array)

Building on your knowledge of 2D arrays, you can create arrays of strings:

// Method 1: 2D character array char names[5][20]; // 5 strings, each up to 19 characters + '\0' // Method 2: Initialize with values char cities[][15] = {"Delhi", "Mumbai", "Bangalore", "Chennai"}; // Method 3: Array of string pointers (advanced) char *languages[] = {"C", "Python", "Java", "JavaScript"};

Array of Strings Example:

#include <stdio.h> #include <string.h> int main() { char subjects[4][20]; char predefined[][15] = {"Mathematics", "Physics", "Chemistry"}; // Input strings printf("Enter 4 subject names:\n"); for (int i = 0; i < 4; i++) { printf("Subject %d: ", i + 1); scanf("%s", subjects[i]); } // Display strings printf("\nYou entered:\n"); for (int i = 0; i < 4; i++) { printf("%d. %s\n", i + 1, subjects[i]); } // Display predefined strings printf("\nPredefined subjects:\n"); for (int i = 0; i < 3; i++) { printf("%s\n", predefined[i]); } return 0; }

Common String Operations

1. Palindrome Check

#include <stdio.h> #include <string.h> #include <ctype.h> int isPalindrome(char str[]) { int start = 0; int end = strlen(str) - 1; while (start < end) { if (tolower(str[start]) != tolower(str[end])) { return 0; // Not a palindrome } start++; end--; } return 1; // Is a palindrome } int main() { char word[100]; printf("Enter a word: "); scanf("%s", word); if (isPalindrome(word)) { printf("%s is a palindrome\n", word); } else { printf("%s is not a palindrome\n", word); } return 0; }
Enter a word: radar radar is a palindrome

2. Vowel and Consonant Count

#include <stdio.h> #include <ctype.h> void countVowelsConsonants(char str[]) { int vowels = 0, consonants = 0, others = 0; for (int i = 0; str[i] != '\0'; i++) { char ch = tolower(str[i]); if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels++; } else { consonants++; } } else { others++; } } printf("Vowels: %d\n", vowels); printf("Consonants: %d\n", consonants); printf("Other characters: %d\n", others); } int main() { char sentence[200]; printf("Enter a sentence: "); fgets(sentence, 200, stdin); countVowelsConsonants(sentence); return 0; }
Enter a sentence: Hello World! Vowels: 3 Consonants: 7 Other characters: 2

3. Word Count in a Sentence

#include <stdio.h> #include <ctype.h> int countWords(char str[]) { int count = 0; int inWord = 0; for (int i = 0; str[i] != '\0'; i++) { if (isalpha(str[i])) { if (!inWord) { count++; inWord = 1; } } else { inWord = 0; } } return count; } int main() { char text[200]; printf("Enter a sentence: "); fgets(text, 200, stdin); printf("Number of words: %d\n", countWords(text)); return 0; }
Enter a sentence: C Programming is fun and challenging Number of words: 5

Memory Representation of Strings

Understanding how strings are stored in memory helps in better programming:

Memory Layout Example:

#include <stdio.h> int main() { char str[] = "UPES"; printf("String: %s\n", str); printf("Memory layout:\n"); for (int i = 0; i <= 4; i++) { // Include null terminator if (str[i] == '\0') { printf("Position %d: '\\0' (ASCII: %d)\n", i, str[i]); } else { printf("Position %d: '%c' (ASCII: %d)\n", i, str[i], str[i]); } } return 0; }
String: UPES Memory layout: Position 0: 'U' (ASCII: 85) Position 1: 'P' (ASCII: 80) Position 2: 'E' (ASCII: 69) Position 3: 'S' (ASCII: 83) Position 4: '\0' (ASCII: 0)
Memory Insight: The string "UPES" actually requires 5 bytes of memory: 4 for the characters and 1 for the null terminator ('\0').

Common String Errors and Best Practices

Common Errors:

  1. Buffer Overflow: Writing beyond the allocated string size
  2. Missing Null Terminator: Forgetting '\0' in manual initialization
  3. Using gets(): Unsafe input function
  4. Comparing with ==: Use strcmp() instead
  5. Modifying String Literals: String constants cannot be modified

Best Practices:

  • Always use fgets() for safe string input
  • Check array bounds when manipulating strings
  • Use string library functions instead of writing your own
  • Initialize string arrays properly
  • Be careful with string concatenation and copying

Safe String Operations:

#include <stdio.h> #include <string.h> int main() { char source[50] = "Hello"; char destination[100]; char buffer[50]; // Safe copying strncpy(destination, source, sizeof(destination) - 1); destination[sizeof(destination) - 1] = '\0'; // Ensure null termination // Safe concatenation strncat(destination, " World", sizeof(destination) - strlen(destination) - 1); // Safe input printf("Enter your name: "); fgets(buffer, sizeof(buffer), stdin); // Remove newline from fgets if present char *newline = strchr(buffer, '\n'); if (newline) *newline = '\0'; printf("Result: %s\n", destination); printf("Name: %s\n", buffer); return 0; }

Practice Problems

Problem 1: String Statistics

Write a program that takes a string input and displays:

  • Total length
  • Number of uppercase letters
  • Number of lowercase letters
  • Number of digits
  • Number of special characters

Problem 2: String Sorting

Create a program that reads 5 names and sorts them in alphabetical order using string comparison functions.

Problem 3: String Search

Implement a function that searches for a substring within a main string and returns the position of the first occurrence.

Problem 4: String Encryption

Write a program that implements Caesar cipher encryption (shift each character by a fixed number of positions).

Summary and Key Takeaways

  • Strings are character arrays terminated by null character ('\0')
  • Always include <string.h> for string manipulation functions
  • Use fgets() for safe string input, avoid gets()
  • String comparison requires strcmp(), not == operator
  • Be mindful of buffer sizes to prevent overflow
  • Strings can be processed character by character using loops
  • Array of strings is implemented as 2D character arrays
  • String literals are stored in read-only memory
Connection to Future Topics: Understanding strings is crucial for file handling, command-line arguments, and advanced data structures that you'll learn in upcoming lectures.

Next Lecture Preview

Lecture 14: Introduction to Functions

In the next lecture, we'll explore:

  • Function definition and declaration
  • Parameter passing techniques
  • Return values and scope
  • Passing arrays and strings to functions
  • Recursive functions