Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

🌙