Course Topics
C Basics Introduction and Setup Syntax and Program Structure Comments and Documentation Compiling and Running C Programs Exercise Variables and Data Types Variables and Declaration Data Types (int, float, char, double) Constants and Literals Type Conversion and Casting Exercise Operators Arithmetic Operators Comparison Operators Logical Operators Assignment Operators Bitwise Operators Exercise Input and Output Standard Input/Output (scanf, printf) Format Specifiers File Input/Output Exercise Control Flow - Conditionals If Statements If-Else Statements Switch Statements Nested Conditionals Exercise Control Flow - Loops For Loops While Loops Do-While Loops Loop Control (break, continue) Nested Loops Exercise Functions Defining Functions Function Parameters and Arguments Return Statements Scope and Variables Recursion Exercise Arrays One-Dimensional Arrays Multi-Dimensional Arrays Array Operations Strings as Character Arrays Exercise Pointers Introduction to Pointers Pointer Arithmetic Pointers and Arrays Pointers and Functions Dynamic Memory Allocation Exercise Strings String Handling String Functions (strlen, strcpy, strcmp) String Manipulation Exercise Structures Defining Structures Structure Members Arrays of Structures Pointers to Structures Exercise File Handling Opening and Closing Files Reading from Files Writing to Files File Positioning Exercise Memory Management Static vs Dynamic Memory malloc() and free() Memory Leaks Best Practices Exercise Advanced Topics Preprocessor Directives Macros Header Files Modular Programming Exercise Final Project Project Planning Building Complete Application Code Organization Testing and Debugging Exercise

String Manipulation

Introduction

Welcome to the topic of String Manipulation in C language! This topic is crucial as it equips you with the skills to work with text data, a common and essential aspect of programming. By the end of this lesson, you'll be able to create, modify, compare, search, and manipulate strings efficiently.

Core Concepts

Strings in C are arrays of characters terminated by a null character \0. String literals are enclosed within double quotes ("). Let's explore some key terms:

  • Character Array: An array containing characters, with the last one being the null character.
  • String Length: The number of characters in a string excluding the terminating null character. You can find it using strlen(string).
  • Substring: A portion of a string obtained by selecting consecutive characters. You can access substrings using array indexing, i.e., substring[start:end].
  • String Concatenation: Combining two strings into one. In C, you can use the strcat(destination, source) function or the addition operator (+).

Practical Examples

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

int main() {
  char string1[20] = "Hello";
  char string2[20] = "World";
  char combined[40];

  // Concatenation with strcat()
  strcat(combined, string1);
  strcat(combined, " ");
  strcat(combined, string2);
  printf("Combined: %s\n", combined);

  // Concatenation with addition operator
  char concat[40];
  concat[0] = string1[0];
  concat[1] = ' ';
  int i;
  for (i = 0; string2[i] != '\0'; ++i) {
    concat[i + 1] = string2[i];
  }
  concat[i + 1] = '\0';
  printf("Combined (with addition operator): %s\n", concat);

  return 0;
}

In this example, we combine two strings using strcat() and the addition operator. The output will be:

Combined: Hello World
Combined (with addition operator): Hello World

Common Issues and Solutions

Buffer Overflow Error

What causes it: Writing more characters to a string than its allocated buffer size.

#include <stdio.h>

int main() {
  char str[5] = "Hello";
  // Adding one extra character causes a buffer overflow error
  strcat(str, " World");
}

Error message:

Segmentation fault (core dumped)

Solution: Ensure that you allocate sufficient space for your string and avoid writing beyond the buffer's limit.

Why it happens: The program tries to write more characters than the allocated buffer can hold, causing memory corruption and a segmentation fault.

How to prevent it: Allocate enough memory for your strings or dynamically resize them when necessary using functions like realloc().

Null Character Missing Error

What causes it: Failing to include a null character at the end of a string.

#include <stdio.h>

int main() {
  char str[5] = "Hello";
  // Forgetting the null character makes it a string literal instead of a character array
  printf("%s\n", str);
}

Error message: None in this example because it's a syntax error, not a runtime error.

Solution: Always end your strings with a null character ('\0').

Why it happens: Leaving out the null character makes it impossible for the program to determine the string's length and causes issues when using functions that rely on the null character, such as strlen().

How to prevent it: Be mindful of adding a null character at the end of your strings. You can use the sprintf() function with a format string like "%s\0" to ensure the last character is always a null character.

Comparing Strings

What causes it: Comparing strings lexicographically instead of comparing their addresses in memory.

#include <stdio.h>

int main() {
  char str1[5] = "Hello";
  char str2[5] = "Hello";
  printf("%d\n", str1 == str2); // Outputs: 1 (true) due to comparison by address, not lexicography
}

Error message: None in this example because it's a logic error.

Solution: Use strcmp() or strcasecmp() to compare strings lexicographically instead of comparing their addresses in memory.

Why it happens: Comparing strings using the == operator compares their addresses, not their contents. The two strings might be located at different memory addresses even if they have identical content.

How to prevent it: Use functions like strcmp() or strcasecmp() to compare strings based on their lexicographical order.

Best Practices

  • Use proper error handling when dealing with string manipulation functions that return error codes, such as malloc().
  • Be cautious when copying strings using functions like strcpy(), as it does not include a null character at the end of the destination string by default. Consider using strncpy(destination, source, size) to ensure the destination array is properly terminated.
  • Use strlen() to check the length of strings before performing operations, especially when dealing with user input or dynamically allocated memory.

Key Takeaways

  • Understand the core concepts of string manipulation in C: character arrays, string length, substrings, and concatenation.
  • Be aware of common errors such as buffer overflow, null character missing, and comparing strings by address instead of lexicography.
  • Follow best practices for safe and efficient string manipulation: proper error handling, careful copying, and checking the length of strings.

Next steps for learning include exploring more advanced topics like regular expressions and searching algorithms for strings. Happy coding!