Day 4 of the 100 Days DSA Challenge: String Basics

Welcome to Day 4 of the 100 Days DSA (Data Structures and Algorithms) Challenge! Today, we’ll delve into the basics of strings and explore some fundamental operations. Strings are essential in programming, and mastering these basics will pave the way for more advanced string manipulations.

1. Write a Program to Reverse a String

Reversing a string involves changing the order of characters such that the last character becomes the first, the second-last becomes the second, and so on.

Placeholder for Code Example:

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

void reverseString(char str[]) {
    int n = strlen(str);
    for (int i = 0; i < n / 2; i++) {
        char temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }
}
int main() {
    char str[] = "Rishi!";
    reverseString(str);
    printf("Reversed string: %s\n", str);
    return 0;
}

2. Check if a Given String is a Palindrome

A palindrome is a string that reads the same backward as forward. Checking for a palindrome involves comparing characters from both ends towards the center.

Placeholder for Code Example:

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

bool isPalindrome(char str[]) {
    int n = strlen(str);
    for (int i = 0; i < n / 2; i++) {
        if (str[i] != str[n - i - 1]) {
            return false;
        }
    }
    return true;
}
int main() {
    char str[] = "malayalam";
    if (isPalindrome(str)) {
        printf("The string is a palindrome.\n");
    } else {
        printf("The string is not a palindrome.\n");
    }
    return 0;
}

3. Write a Function to Count Vowels and Consonants in a String

Counting vowels and consonants helps in analyzing the composition of the string. Vowels in English are a, e, i, o, u (and sometimes y), while consonants are the rest of the alphabet.

Placeholder for Code Example:

#include <stdio.h>
#include <ctype.h>

void countVowelsnConson(char str[],int *vowels,int *consonants) {
    *vowels = 0;
    *consonants = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        char ch = tolower(str[i]);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            (*vowels)++;
        } else if (isalpha(ch)) {
            (*consonants)++;
        }
    }
}
int main() {
    char str[] = "Hello, World!";
    int vowels, consonants;
    countVowelsnConson(str, &vowels, &consonants);
    printf("Vowels: %d\n", vowels);
    printf("Consonants: %d\n", consonants);
    return 0;
}

4. Implement a Program to Remove Duplicate Characters from a String

Removing duplicate characters ensures that each character appears only once in the string, maintaining the order of first appearance.

Placeholder for Code Example:

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

void removeDuplicates(char str[]) {
    bool seen[256] = { false };
    int n = strlen(str);
    int index = 0;

    for (int i = 0; i < n; i++) {
        if (!seen[(unsigned char)str[i]]) {
            seen[(unsigned char)str[i]] = true;
            str[index++] = str[i];
        }
    }
    str[index] = '\0';
}
int main() {
    char str[] = "Hello World!";
    removeDuplicates(str);
    printf("String after removing duplicates: %s\n", str);
    return 0;
}

5. Solve the "Longest Word in a String" Problem

Finding the longest word in a string involves splitting the string by spaces and comparing the lengths of the resulting words.

Placeholder for Code Example:

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

void longestWord(char str[], char result[]) {
    int maxLen = 0, currentLen = 0;
    char *word = strtok(str, " ");
    while (word != NULL) {
        currentLen = strlen(word);
        if (currentLen > maxLen) {
            maxLen = currentLen;
            strcpy(result, word);
        }
        word = strtok(NULL, " ");
    }
}
int main() {
    char str[] = "Find the longest word in this string";
    char result[50];
    longestWord(str, result);
    printf("Find the Longest word: %s\n", result);
    return 0;
}

Conclusion

Today's challenges are designed to strengthen your understanding of string manipulations and basic operations. By working through these exercises, you'll develop valuable problem-solving skills and gain confidence in handling strings. Keep up the great work, and see you tomorrow for Day 5 of the 100 Days DSA Challenge!