Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

🌙