C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • 1. Searching Strings:
  • 2. Tokenizing Strings:
  • 3. Analyzing Strings:

Was this helpful?

  1. Strings

Searching, Tokenizing, and Analyzing Strings

In C programming, searching, tokenizing, and analyzing strings are common tasks performed to manipulate and extract information from strings. Let's explore these operations and how they can be implemented using C.

1. Searching Strings:

Searching for a substring within a string can be done using functions from the <string.h> library. The strstr function can be used to find the first occurrence of a substring in a string.

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

int main() {
    const char *mainString = "Hello, World!";
    const char *substring = "World";

    // Search for the substring
    char *result = strstr(mainString, substring);

    if (result != NULL) {
        printf("Substring found at index %ld\n", result - mainString);
    } else {
        printf("Substring not found\n");
    }

    return 0;
}

This program searches for the substring "World" within the main string "Hello, World!" and prints the index where it is found.

2. Tokenizing Strings:

Tokenization involves breaking a string into smaller parts or tokens. The strtok function from the <string.h> library can be used for this purpose.

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

int main() {
    char input[] = "apple,orange,banana";
    const char delimiter[] = ",";

    // Tokenize the string
    char *token = strtok(input, delimiter);

    while (token != NULL) {
        printf("Token: %s\n", token);
        token = strtok(NULL, delimiter);
    }

    return 0;
}

This program tokenizes the input string "apple,orange,banana" using a comma (,) as the delimiter and prints each token.

3. Analyzing Strings:

Analyzing strings involves extracting information such as length, counting occurrences, and converting case. Here's an example that counts the number of occurrences of a specific character:

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

int main() {
    const char *text = "programming is fun!";
    char targetChar = 'm';
    int count = 0;

    // Count occurrences of the target character
    for (int i = 0; i < strlen(text); i++) {
        if (text[i] == targetChar) {
            count++;
        }
    }

    printf("Occurrences of '%c': %d\n", targetChar, count);

    return 0;
}

This program counts the occurrences of the character 'm' in the string "programming is fun!"

These operations provide a foundation for more complex string manipulation tasks. Understanding and combining these techniques can help in solving various problems related to string handling in C.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousProgram: Bubble SortNextConverting Strings

Was this helpful?