dailycodebase

2 month data structures and algorithmic scripting challenge starting from 20th December 2018 - Coding is Fun! 💯💯 Do it everyday!! Also, Do give us a ⭐ if you liked the repository

View on GitHub

cover

Day 6

Questions for today

Welcome back to the Day 6 of Daily Codes, as you might have noticed we are slowly stepping up our game in Strings. So today let’s do 3 cool questions that seems pretty straightforward while reading the question but can be a little challenging when you actually try to solve them

Part A - Sentence Capitalization

Question - Write a program to capitalize the first letter of each word in the string.

Part B - Word Reversal

Question - Given a sentence, Write a program to reverse each word in it.

Part C - Anagram Check

Question - Write a program to check whether the two provided strings are anagrams of each other. (Do not ignore the letter case)

ques

Part A – Sentence Capitalization

Question - Write a program to capitalize the first letter of each word in the string.

JavaScript Implementation

Solution 1

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

function capitalize (str) {
    // spit the string to array
    let wordArr = str.split(' ');

    // loop through each element of array and capitalize the first letter

    for (let i=0; i<wordArr.length; i++) {

        // Some people might proceed like this, but strings are immutable
        // let currentWord = wordArr[i];
        // currentWord[0] = currentWord[0].toUpperCase();
        // console.log(currentWord);

        // Append the first letter (capitalized)
        let currentWord = '';
        currentWord += wordArr[i][0].toUpperCase();

        // Append the rest of the word (can be easily done by String slice)
        for (let j=1; j<wordArr[i].length; j++) {
            currentWord += wordArr[i][j];
        }

        // replace the current word by currentWord
        wordArr[i] = currentWord;
    }

    // join the array into string
    return wordArr.join(' ');
}

console.log(capitalize ('hello world'));

Solution 2

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

function capitalize (str) {
    // declare an empty array
    let capitalied = [];

    // loop through each word and capitalize the first letter
    for (let word of str.split(' ')) {
        capitalied.push(word[0].toUpperCase() + word.slice(1));
    }

    // join the array into string
    return capitalied.join(' ');
}

Solution 3

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

 function capitalize (str) {
    let result = str[0].toUpperCase();

    for (let i=1; i<str.length; i++) {
        if (str[i-1] === ' ') {
            result += str[i].toUpperCase();
        } else {
            result += str[i];
        }
    }

    return result;
}

Java Implementation

Solution 1

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

import java.util.Scanner;

public class SentenceCap1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the sentence: ");
        String str = input.nextLine();

        String[] words = str.split("\\s+");

        for (int i=0; i<words.length; i++) {
            words[i] = Character.toUpperCase(words[i].charAt(0)) + words[i].substring(1, words[i].length());
        }

        // Join the array
        System.out.print(String.join(" ", words));
    }
}

Solution 2

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

import java.util.Scanner;

public class SentenceCap2 {
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter the sentence: ");
        String sentence = input.nextLine();
        String capitalized = "";

        for (int i=0; i<sentence.length(); i++) {
            if (i==0)   capitalized += Character.toUpperCase(sentence.charAt(i));
            else {
                if (sentence.charAt(i-1) == ' ') {
                    capitalized  += Character.toUpperCase(sentence.charAt(i));
                } else {
                    capitalized += sentence.charAt(i);
                }
            }
        }

        // Print the results
        System.out.println("Capitalized String is: " + capitalized);
    }
}

Python Implementation

Solution

"""
  * @author : ashwek
  * @date : 27/12/2018
"""

def Cap(String):
    String = list(String)
    String[0] = String[0].upper()

    for i in range(1, len(String)):
        if(String[i] == ' ' and i < len(String)-1 ):
            String[i+1] = String[i+1].upper()

    return "".join(String)

Str = input("Enter a string = ")

print("Capitalize words = ", Cap(Str))

sentence_capitalization.py

'''
@author: aaditkamat
@date: 27/12/2018
'''

#short version
from string import capwords
def capitalize_sentence_short(string):
    return capwords(string)

#slightly long version
def capitalize_sentence_long(string):
    new_string = ''
    for word in string.split(' '):
        new_string += word.capitalize() + ' '
    return new_string

print("Enter a string: ", end= ' ')
string = input()
print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string))
print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string))

C++ Implementation

Solution

/**
 * @author Bhanu0202
 * @date 27/12/2018
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
	string sa;
	getline(cin, sa);
	int l = sa.length();
	if(sa[0]>=97 && sa[0]<=122)
	sa[0] -= 32;
	for(int i = 0; i < l; i++){
	    if(sa[i] == 32 && sa[i + 1]>=97 && sa[i + 1]<=122){
	        sa[i + 1] -= 32;  
	    }
	}
	cout << sa;
	return 0;
}

Solution

/*
* @author : dhruv-gupta14
* @date : 27/12/2018
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    
    string ans;
    ans = toupper(s[0]);
    for(int i=1; i < s.size(); i++)
    {
        if(s[i-1] == ' ')
        {
            ans += toupper(s[i]);
        } else{
            ans += s[i];
        }
    }
    
    cout << ans;
    return 0;
}

Sentance Capatalization by @imkaka


/*
* @author : imkaka
* @date   : 27/12/2018
*/

#include<iostream>
#include<string>

using namespace std;

int main(){
    string str;
    getline(cin, str);

    str[0] = toupper(str[0]);
    for(int i = 1; i < str.size(); ++i){
        if(str[i-1] == ' '){
            str[i] = toupper(str[i]);
        }
    }

    cout << "Result: " << str;
    return 0;
}

Sentence Capatalization by @divyakhetan

/**
 * @author:divyakhetan
 * @date: 30/12/2018
 */


#include<bits/stdc++.h>
using namespace std;


int main(){
	string s;
	getline(cin, s);
	s[0] = toupper(s[0]);
	for(int i = 1; i < s.length(); i++){
		if(s[i - 1] == ' '){
			if(s[i] >= 'a' && s[i] <= 'z') s[i] =  toupper(s[i]);
		}
	}
	
	cout << s << endl;
	return 0;
}

Ruby Implementation

sentence_capitalization.rb


=begin
@author: aaditkamat
@date: 27/12/2018
=end

def capitalize_sentence(string)
    new_string = ''
    string.split(' ').each do |word|
        new_string += word.capitalize + ' '
    end
    new_string
end

print"Enter a string: "
string = gets().chomp
puts "String \" #{string} \" with first letter of each word capitalized: #{capitalize_sentence(string)}"

Part B – Word Reversal

Question - Given a sentence, Write a program to reverse each word in it.

JavaScript Implementation

Solution 1

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

function wordRev (line) {
    // Split the line
    let words = line.split(' ');

    // reverse each word
    for (let i=0; i<words.length; i++) {
        words[i] = words[i].split('').reverse().join('');
    }

    // Join and return
    return words.join(' ');
}

console.log (wordRev("hello world Greetings"));

Solution 2

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

 function wordRev (line) {
    let reversed = [];

    for(let word of line.split(' ')) {
        reversed.push(word.split('').reverse().join(''));
    }

    return reversed.join(' ');
}

console.log (wordRev("  hello  wow   world Greetings"));

Solution 3

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

// Without inbuilt reverse or split, a straightforward method

function wordRev (line) {
    // iterate through the string and add each word in a new array (splitting it on white space)
    let words = [], count = 0, word = '';

    for (let i=0; i<line.length; i++) {
        if (line[i] !== ' ') {
            word += line[i];
        } else {
            words[count] = word;
            word = '';
            count++;
        }
    }
    // Add the last word as well to the words array as well
    words[count] = word;
    count++;

    // Reverse the words
    let temp;
    for (let i=0; i<count; i++) {
        temp = '';
        for (let j=words[i].length-1; j>=0; j--) {
            temp += words[i][j];
        }
        words[i] = temp;
    }

    // join the elements (Ok, let's not use the traditional join() method -_-)
    let reversed = '';
    for (let i=0; i<count; i++) {
        if (i!=count-1) {
            reversed += words[i] + ' ';
        } else {
            reversed += words[i];
        }
    }

    // print the result
    return reversed;
}

console.log(wordRev ('hello world greetings'));

Java Implementation

Solution

/**
 * @author MadhavBahl
 * @date 27/12/2018
 */

import java.util.Scanner;
import java.lang.*;

public class WordRev {
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter the sentence: ");
        String sentence = input.nextLine();

        String[] words = sentence.split("\\s+");
        String reversed;

        for (int i=0; i<words.length; i++) {
            reversed = "";
            for (int j=0; j<words[i].length(); j++) {
                reversed = words[i].charAt(j) + reversed;
            }
            words[i] = reversed;
        }

        String wordsReversed = String.join(" ", words);
        System.out.println("String with reversed words: " + wordsReversed);
    }
}

Python Implementation

Solution

"""
  * @author : ashwek
  * @date : 27/12/2018
"""

def rev(Str, start, end):

    for i in range(0, (end-start)//2):
        Str[i+start], Str[end-i-1] = Str[end-i-1], Str[i+start]

def wordRev(Str):

    Str = list(Str)
    start = 0

    while( True ):

        try: end = Str.index(' ', start)
        except ValueError: break

        rev(Str, start, end)
        start = end+1

    rev(Str, start, len(Str))

    return ''.join(Str)


Str = input("Enter a string = ")

print("Words Reversed =", wordRev(Str))

reverse_words.py

'''
@author: aaditkamat
@date: 27/12/2018
'''

def reverse_words(string):
    new_string = ''
    for word in string.split(' '):
        new_string += word[::-1] + ' '
    return new_string

print("Enter a string: ", end= '')
string = input()
print("Reverse of string \'", string, "\': ", reverse_words(string), sep='')

C++ Implementation

Solution

/**
 * @author Bhanu0202
 * @date 27/12/2018
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	string sa, sb;
	getline(cin, sa);
	int l = sa.length();
	vector<string> s;
	s.push_back("");
	int c = 0;
	for(int i = 0; i < l; i++){
    if(sa[i] == ' '){
        c++;
        s.push_back("");
    }
    else
        s[c] += sa[i];
    }
    for(int i = 0; i <= c; i++){
        reverse(s[i].begin(), s[i].end());
        sb += s[i]  + " ";
    }
    cout << sb;
	return 0;
}

Solution by @dhruv-gupta14

/*
* @author : dhruv-gupta14
* @date : 27/12/2018
*/

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string str;
    getline(cin,str);
    
    string ans;
    stringstream s(str);
    
    while(s >> ans)
    {
        reverse(ans.begin(),ans.end());
        cout << ans << " ";
    }
    return 0;
}

Word Reverse by @imkaka

/*
* @author : imkaka
* @date   : 27/12/2018
*/

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>

using namespace std;

int main(){
    string str;
    getline(cin, str);

    stringstream ss(str);
    string res = "";
    do {
        // Read a word
        string word;
        ss >> word;

        reverse(word.begin(), word.end());
        res += word;
        res+=" ";

        // While there is more to read
    } while (ss);

    cout << "Reversed Word Sentance => { " << res << " }" << endl;
    return 0;
}

Word Reverse by @divyakhetan

/**
 * @author:divyakhetan
 * @date: 30/12/2018
 */


#include<bits/stdc++.h>
using namespace std;


int main(){
	string s;
	getline(cin, s);
	s[0] = toupper(s[0]);
	for(int i = 1; i < s.length(); i++){
		if(s[i - 1] == ' '){
			if(s[i] >= 'a' && s[i] <= 'z') s[i] =  toupper(s[i]);
		}
	}
	
	cout << s << endl;
	return 0;
}

Ruby Implementation

reverse_words.rb


=begin
@author: aaditkamat
@date: 27/12/2018
=end

def reverse_words(string)
    new_string = ''
    string.split(' ').each do |word|
        new_string += word.reverse + ' '
    end
    new_string
end

print"Enter a string: "
string = gets().chomp
print"Reverse of string #{string}:  #{reverse_words(string)}"

Part C – Anagram Check

Question - Write a program to check whether the two provided strings are anagrams of each other.

JavaScript Implementation

Solution 1

/**
 * @author MadhavBahl
 * @date 27/12/2018
 * METHOD -- Check the lengths of both strings, sort them and then check whether they are same
 */

function anagram (str1, str2) {
    let len1 = str1.length,
        len2 = str2.length;
    
    // Compare lengths
    if (len1 !== len2) {
        console.log ('Invalid Input');
        return -1;
    }

    // sort the strings
    let sortedStr1 = str1.split('').sort().join(''),
        sortedStr2 = str2.split('').sort().join('');

    // Compare both strings
    if (sortedStr1 === sortedStr2) {
        console.log(`"${str1}" and "${str2}" are Anagrams`);
        return 1;
    } else {
        console.log(`"${str1}" and "${str2}" are not Anagrams`);
        return 0;
    }
}

anagram ('LISTEN', 'SILENT');

Solution 2

/**
 * @author MadhavBahl
 * @date 27/12/2018
 * METHOD -- Prepare 2 objects which stores frequency of every character in both strings, compare those 2 objects  (dictionaries in python)
 */

function anagram (str1, str2) {
    let len1 = str1.length,
        len2 = str2.length;
    
    // Compare lengths
    if (len1 !== len2) {
        console.log ('Invalid Input');
        return -1;
    }

    // Make  frequency objects
    let countObj1 = {},
        countObj2 = {};

    for (let i=0; i<len1; i++) {
        countObj1[str1[i]] = countObj1[str1[i]] + 1 || 1;
    }

    for (let i=0; i<len2; i++) {
        countObj2[str2[i]] = countObj2[str2[i]] + 1 || 1;
    }

    // compare frequency objects
    // Please note that there is no direct way of comparing 2 objects.
    // We can either use some librries like Lowdash, or we can check the equality of each key value pair in objects, which is indeed a tedious task, but still, lets do it :)
    for (let key in countObj1) {
        if (countObj1[key] !== countObj2[key]) {
            console.log(`"${str1}" and "${str2}" are not Anagrams`);
            return 0;
        }
    }

    console.log(`"${str1}" and "${str2}" are Anagrams`);
}   

anagram ('LISTEN', 'MILENT');

Solution 3

/**
 * @author MadhavBahl
 * @date 27/12/2018
 * A simple method which first compares the lengths of strings and then iterates through the characters of any string and check whether it exists in the other one as well and does same for the other string
 * Please note that this is not at all an efficient method. Do not use this.
 */

function anagram (str1, str2) {
    let len1 = str1.length,
        len2 = str2.length;

    // Lengths of both strings must be same
    if (len1 !== len2) {
        console.log ('Invalid Input');
        return -1;
    }

    // check characters of string 1 are there in string 2
    let flag = 1;
    for (let char of str1) {
        if (str2.indexOf(char) < 0) {
            flag = 0;
            break;
        }
    }

    if (flag !== 1) {
        console.log (`${str1} and ${str2} are not Anagrams`);
        return 0;
    }

    for (let char of str2) {
        if (str1.indexOf(char) < 0) {
            flag = 0;
            break;
        }
    }

    if (flag !== 1) {
        console.log (`${str1} and ${str2} are not Anagrams`);
        return 0;
    } 
    else {
        console.log (`${str1} and ${str2} are Anagrams`);
        return 1;
    }
}

anagram ('LISTEN', 'SILENT');

Python Implementation

Solution

"""
  * @author : ashwek
  * @date : 27/12/2018
"""

Str1 = input("Enter string 1 = ")
Str2 = input("Enter string 2 = ")

print(Str1, "&", Str2, "are", end=" ")
if( sorted(Str1) != sorted(Str2) ): print("not", end=" ")
print("anagrams")

anagram_check.py

'''
@author: aaditkamat
@date: 27/12/2018
'''

def check_anagram(first_str, second_str):
    first_word_dict = {}
    second_word_dict = {}
    first_str = first_str.replace(' ', '').lower()
    second_str = first_str.replace(' ', '').lower()
    for ch in second_str:
        if ch not in second_word_dict:
            second_word_dict[ch] = 1
        else:
            second_word_dict[ch] += 1
    return first_word_dict == second_word_dict

print("Enter two strings: ")
first_str = input()
second_str = input()
print("Are ", first_str, "and ", second_str, "anagrams? ", check_anagram(first_str, second_str))

C++ Implementation

Solution

/**
 * @author Bhanu0202
 * @date 27/12/2018
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
	string sa, sb;
	cin >> sa >>sb;
	int la = sa.length();
	int lb = sb.length();
	int count[26] = {0};
	if(la != lb){
	    cout << "Invalid Input";
	}
	else{
	    int flag = 0;
	    sort(sa.begin(), sa.end()); 
	    sort(sb.begin(), sb.end());
	    if(sa != sb){
	        flag = 1;
	    }
	    if(flag == 0)
	    cout << "Anagrams";
	    else
	    cout << "Not anagrams";
	}
	return 0;
}

Solution by @dhruv-gupta14

/*
* @author : dhruv-gupta14
* @date : 27/12/2018
*/

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int flag=0;
    string str1;
    getline(cin,str1);
    
    string str2;
    getline(cin,str2);
    
    int n = str1.length();
    int m = str2.length();
    
    if(n!=m)
    {
        cout << "Not Anagrams";
    } else{
        sort(str1.begin(), str1.end());
        sort(str2.begin(), str2.end());
    
        for(int i=0; i<n; i++)
        {
            if(str1[i] != str2[i])
            {
                cout << "Not Anagrams";
                flag = 1;
                break;
            }
        }
        if(flag == 0)
        {
            cout << "Anagrams";
        }
    }
    return 0;
}

Anagram Check by @imkaka

/*
* @author : imkaka
* @date   : 27/12/2018
*/

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int main(){
    string str1, str2;
    cin >> str1 >> str2;

    string temp1 = str1, temp2 = str2;
    if(str1.size() != str2.size()){
        cout << temp1 << " and " << temp2 << " are NOT Anagrams of each other!!" << endl;
    }
    else{
        sort(str1.begin(), str1.end());
        sort(str2.begin(), str2.end());

        if(str1 == str2){
            cout << temp1 << " and " << temp2 << " are Anagrams of each other!!" << endl;
        }
        else{
            cout << temp1 << " and " << temp2 << " are NOT Anagrams of each other!!" << endl;
        }
    }

    return 0;
}

Anagram Check by @divyakhetan

/**
 * @author:divyakhetan
 * @date: 30/12/2018
 */


#include<bits/stdc++.h>
using namespace std;


int main(){
	string s1, s2;
	cin >> s1 >> s2;
	if(s1.length() != s2.length()) cout << "Not of same length";
	else{
		sort(s1.begin(), s1.end());
		sort(s2.begin(), s2.end());
		if(s1.compare(s2) == 0) cout << "Anagrams!";
		else cout << "Not anagrams";
	} 
	return 0;
}

Ruby Implementation

anagram_check.rb


=begin
@author: aaditkamat
@date: 27/12/2018
=end

def check_anagram(first_str, second_str)
    first_word_dict = {}
    second_word_dict = {}
    first_str.gsub!(" ", "").downcase!
    second_str.gsub!(" ", "").downcase!
    first_str.each_char do |ch|
        if first_word_dict.has_key?(ch)
            first_word_dict[ch] += 1
        else
            first_word_dict[ch] = 1
        end
    end
    second_str.each_char do |ch|
        if second_word_dict.has_key?(ch)
            second_word_dict[ch] += 1
        else
            second_word_dict[ch] = 1
        end
    end
    first_word_dict == second_word_dict
end

puts "Enter two strings: "
first_str = gets().chomp
second_str = gets().chomp
puts "\nAre #{first_str} and #{second_str} anagrams? #{check_anagram(String.new(first_str), String.new(second_str))}"

Have Another solution?

The beauty of programming lies in the fact that there is never a single solution to any problem.

In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase) :)