Category Archives: Strings

View algorithms on Strings

Easiest way to change case of alphabets

Here we will discuss the easiest and most simple way to change case of alphabets. Hint : We will be using bit hacks. Often during our programming needs we need to change the case of alphabets. This can be a problem requirement or simply our need to solve the question. There are several ways to do it, but… Read More »

Longest substring without repeating characters

Question: Given a string, find the length of the longest substring without repeating characters. Input: “abcabcbb” Output: “abc” Input: “bbbb” Output: “b” The longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1. We definitely have the brute-force method, where we find… Read More »

Reverse a string without using Recursion

Question: Write a program in C to reverse a string using recursion Input: Hello Everyone Output: enoyrevE olleH We discussed the method to reverse the string using recursion in this post. But a recursive method is generally not preferred as it takes a longer execution time. This is also a classic interview question and we need to do… Read More »

Find the duplicates in a string.

Question: Find the duplicate characters in a string? Input: floccinaucinihilipilification Output: a ,c ,f ,i ,l ,n ,o The idea for this problem would be:- Create a character array for the number of characters possible. Take 256 for all the ASCII characters as well. Traverse the array and print the characters that have a count greater than 1.… Read More »

Find the number of words in a string.

Question: Find the number of words in a given string? Input: This is a sample    statement       and I need to         find   the number of words. Output: 14 According to the problem, we are given a string and it contain words separated by spaces, next lines or tab spaces. So, the basic idea for the problem would be to… Read More »

Swap strings in C

Question:How will you swap two strings in C? Let us consider the below program. Output of the program is str1 is study, str2 is algorithms. So the above swap() function doesn’t swap strings. The function just changes local pointer variables and the changes are not reflected outside the function. Let us see the correct ways for swapping strings:… Read More »

Print reverse of a string using recursion.

Question: Write a program in C to reverse a string using recursion Input: Hello Everyone Output: enoyrevE olleH We will use the simple concept of recursion to print the string in reversed order. Note that, we will not store the reversed string in any variable, we are just printing the string in reversed order. Recursive function (reversePrint) takes… Read More »

Write a program to change the case of each character in a string

Question: Write a program that changes the case of each character in a string. If its uppercase, convert it to lowercase and vice-versa. Input: ThE quiCK brOwN FOX jUMPs. Output: tHe QUIck BRoWn fox Jumps. In this program we shall apply the basic principle that each character is represented as an integer ASCII code. For more reference look… Read More »