Day 4 of 100 Days of DSA: Strings Basics

Day 4 of 100 Days of DSA: Strings Basics

ยท

3 min read

Strings are one of the most fundamental and frequently used data types in programming. On Day 4 of the 100 Days of DSA challenge, I worked on string manipulation problems, sharpening my skills for handling text-based tasks. Below are the problems and their Java implementations.


1. Reverse a String

The task is to reverse the characters of a string.

We have other alternatives which are far more convenient to reverse a string, but our motive is to learn not to play smart. Which is why I tried to solve the problem using Brute Force Approach.

import java.util.Scanner;

public class ReverseString {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the string to be reversed: ");
        String str = sc.nextLine();
        String ans = "";

        for(int i=0; i<str.length(); i++){
            ans = ans.concat(Character.toString(str.charAt(str.length()-i-1)));
        }

        System.out.println("Reversed String: ".concat(ans));
    }
}


2. Check if a String is a Palindrome

A palindrome reads the same forwards and backwards.

import java.util.Scanner;

public class PalindromeCheck {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the string: ");
        String str = sc.nextLine();
        String rev = "";

        for(int i=0; i<str.length(); i++){
            rev = rev.concat(Character.toString(str.charAt(str.length()-i-1)));
        }

        if(str.equals(rev))
            System.out.println(str.concat(" is a palindrome"));
        else
            System.out.println(str.concat(" is not a palindrome"));
    }
}


3. Count Vowels and Consonants in a String

This involves iterating through the string and classifying characters as vowels or consonants.

import java.util.Scanner;

public class VowelConsonentCount {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the string: ");
        String str = sc.nextLine();
        int count=0;

        for(int i=0; i<str.length(); i++){
            if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u')
                count++;
        }
        System.out.println("Number of vowels: "+count);
        System.out.println("Number of consonents: "+(str.length()-count));
    }
}


4. Remove Duplicate Characters from a String

This task requires creating a new string with only unique characters.

import java.util.Scanner;

public class RemoveDuplicates {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the String: ");
        String str = sc.nextLine();
        String ans = "";

        for(char c : str.toCharArray()){
            if(!ans.contains(Character.toString(c)))
                ans = ans.concat(Character.toString(c));
        }

        System.out.println("String after removing duplicates is: ".concat(ans));
    }
}


5. Find the Longest Word in a String

This involves splitting the string into words and comparing their lengths.

import java.util.Scanner;

public class LongestWord {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the string: ");
        String str = sc.nextLine();
        String longest_word = "";
        String current_word = "";

        for(char c : str.toCharArray()){
            if(c==' ' || c==',' || c=='.' || c=='!' || c=='?'){
                longest_word = (longest_word.length()<current_word.length())?current_word:longest_word;
                current_word = "";
            }else    
                current_word = current_word.concat(Character.toString(c));
        }

        longest_word = (longest_word.length()<current_word.length())?current_word:longest_word;

        System.out.println("Longest word in the given String is: ".concat(longest_word));
    }
}


Reflection

Today's problems were enjoyable and emphasized the importance of string manipulation in solving real-world problems. Learning efficient ways to reverse strings, handle duplicates, and identify patterns was particularly valuable.

Looking forward to Day 5! ๐Ÿš€

ย