Find the duplicates in a string.

    by nikoo28

    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-

    #include <stdio.h>
    
    // prints duplicates in str
    void findDuplicates(char *str)
    {
    	
    	int count[256] = {0};
    	// Scan all characters one by one
        while (*str)
        {
            if (*str == ' ' || *str == '\n' || *str == '\t')
    		{
    			str++;
    			continue;
    		}
    		
    		count[*str]++;
    		str++;
        }
     
        int i;
    	for(i=0; i<256; i++)
    	{
    		if(count[i] > 1)
    			printf("%c ,",i);
    	}
    }
     
    // Driver program to test above function
    int main(void)
    {
        char str[] = "floccinaucinihilipilification";
    
    	findDuplicates(str);
    
    	return 0;
    }
    
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Find the number of words in a string.

    by nikoo28
    2 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. /* Program to count no of words from given input string. */ #include <stdio.h> // returns number …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Different ways to swap 2 numbers.

    by nikoo28
    2 minutes read

    Question: Write a program to swap 2 numbers? Input: a = 4, b = 19 Output: a = 19, b = 4 We will try to discuss the different ways to swap 2 numbers. To understand how swapping works, please visit this post. Method 1 : Using a temporary variable int a = 15; int b = 9; int temp = a; // temp is now 15 a = b; // a is now 9 b = a; // b is now 15 //swapping complete Method 2 : Without a …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Write a program to swap 2 numbers.

    by nikoo28
    3 minutes read

    Question: Write a program to swap 2 numbers? Input: a = 4, b = 19 Output: a = 19, b = 4 Swapping 2 numbers is one of the most basic tasks while programming. But sometimes, we tend to make error in it. For instance look at this code. // function to swap 2 integers void swapIntegers(int a, int b) { // create a temporary variable and interchange the values int temp = a; a = b; b = temp; printf("\nI am inside function\n"); printf("a = %d\n",a); printf("b = %d\n",b); …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted array of n integers that has been rotated an unknown number of times, give a O(log (n)) solution that finds an element in the array? Input: arr [] = {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}. Find 5 . Output: 8 (the index of 5 in the array) We can solve this problem in only one scan of the array as well. Please read Method 1 if you have not done so, as this forms the base of this problem. To …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    Algorithm Analysis – Crash Course

    by nikoo28
    5 minutes read

    We have been discussing the time complexities of each of the algorithm that we have discussed. Let us try to analyze, how these time complexities are calculated. O(1) Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. // set of non-recursive and non-loop statements int a = 5; int b = a * 5; For example swap() function(which interchanges two numbers) has O(1) time complexity. A loop or recursion that runs a …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sequence of integers, find the median of the integers? Input: arr [] = {15, 23, 4, 16, 8, 42}. Find median. Output: 15 Since the question does not specify anything we cannot assume that the given array is a sorted one. The definition of median is the (n/2)th element of a sorted sequence of integers. This definition makes our approach simpler. All we need to do is sort the integers and then return the (n/2)th element. Here is the code for above approach. I use quick sort …

    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