Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

WAP to replace each string of one or more blanks by a single blank in c

Description:

  • Input string:
    • Pointers         are      sharp           knives.
  • Output String:
    • Pointers are sharp knives.
  • Blank can be spaces or tabs. (replace with single space).
Pr-requisites:-
  • Functions
  • Pointers

Objective: -

  • To understand the concept of
    • Functions, Arrays, and Pointers

Inputs: -

  • String with multi-spaces between words
Sample execution: -
Test Case 1:

Enter the string with more spaces in between two words
Pointers     are               sharp     knives.

Pointers are sharp knives. 

Test Case 2:


Enter the string with more spaces in between two words

Welcome                to india

Welcome to india


PROGRAM : 

-----------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include<string.h>

void space(char str[])
{
    int i,k=0;
    while(str[k]!='\0')
    {
        if((str[k]==' ' && str[k+1]==' ') || (str[k]=='\t' && str[k+1]=='\t'))
        {
            i=k;
            while(str[i]!='\0')
            {
                str[i]=str[i+1];
                i++;
            }
             k--;
        }
        k++;
    }
}

int main()
{
    char str[200];
    
   // printf("Enter the string with more spaces in between two words\n");
    scanf("%[^\n]", str);
    
   space(str);
    
    printf("%s\n", str);
}
-----------------------------------------------------------------------------------------------------------------------------


Comments

Popular Posts

🌙