Home Strings
Category:

Strings

View algorithms on Strings

  • 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 what can be more beautiful than using bit hacks for the same. We discusses some of the BIT Hacks in these posts:- Low Level Bit Hacks Some advanced Bit Hacks Here I…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Longest substring without repeating characters

    by nikoo28
    2 minutes read

    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 each string and then compare. But we want to speed up the process. How can we can look up if a character had existed in the substring instantaneously? The answer is using…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Reverse a string without using Recursion

    by nikoo28
    0 minutes read

    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 it in place. We also need to be careful for the null character.

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Find the duplicates in a string.

    by nikoo28
    0 minutes read

    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. Here is the source code for the same-

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Find the number of words in a string.

    by nikoo28
    0 minutes read

    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 traverse the string and increment a counter whenever we see the end of a word.

    0 FacebookTwitterLinkedinWhatsappEmail
  • StringsTheory

    Swap strings in C

    by nikoo28
    1 minutes read

    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: Method 1(Swap Pointers):- If you are using character pointer for strings (not arrays) then change str1 and str2 to point each other’s data. i.e., swap pointers. In a function, if we want…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: We are given an input string, and we need to find the character with the maximum frequency. Input:- “This is an sample string.” Output:- The character with maximum frequency is :- i The logic of this problem is very simple, we need to scan the string and store the frequency of each of the character. For this we can use the ASCII code of the characters which is a separate integer. Note that character are not only the English alphabets. !@#$ are also characters. To extend our program to…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Print reverse of a string using recursion.

    by nikoo28
    1 minutes read

    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 string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way, when pointer reaches ‘\0′, all functions accumulated in stack print char at passed…

    0 FacebookTwitterLinkedinWhatsappEmail
  • 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 into the post – Find the case of a character input by the user. Now we know that ‘a’ -> ASCII CODE = 97 ‘A’ -> ASCII CODE = 65 Thus the…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Write a program that returns the case of the character? Input: e Output: lower case We shall use the concept of ASCII characters to determine the case of the character input by the user. The ASCII table can be found at this link. ASCII TABLE From this we can see the following ASCII codes:- CHARACTER ASCII CODE A 65 Z 90 a 97 z 122 Thus, to check the case we simply compare the ASCII codes. Since we are using C language, in C every character is considered as…

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More