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...

write a program to implement atoi function in c

Description:

    • int my_atoi(const char *s)
      • The function will recieve a string and covert the number stored in the string into exact integer number.
      • Return the number.
Pr-requisites:-
    • Functions
    • Pointers

Objective: -

  • To understand the concept of
    • Functions and Pointers
Inputs: -
          String, String and Integer

Sample execution: -

Test Case 1 :
Enter a numeric string: 12345

String to integer is 12345

Test Case 2 :
Enter a numeric string: -12345

String to integer is -12345

Test Case 3 :
Enter a numeric string: +12345

String to integer is 12345

Test Case 4 :
Enter a numeric string: +-12345

String to integer is 0

Test Case 5 :
Enter a numeric string: 12345-

String to integer is 12345

Test Case 6 :
Enter a numeric string: abcd12345

String to integer is 0

Test Case 7 :
Enter a numeric string: 12345abcd

String to integer is 12345


PROGRAM

----------------------------------------------------------------------------------------------------------------------------


#include <stdio.h>

int my_atoi(const char str[])

{

    int i=0,a=0,b=0,t=0;

    while(str[i]!='\0')

    {

        if(str[0]=='*')

        {

            break;

        }

        if((str[0]=='-' && str[1]=='+') || (str[0]=='+' && str[1]=='-') || (str[0]=='+' && str[1]=='+') || (str[0]=='-' && str[1]=='-'))

        {

            break;

        }

        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))

        {

            break;

        }

        if(str[0]=='-')

        {

            t=1;

         //   i++;

        }

        if(str[i]>='0' && str[i]<='9')

        {

            b=str[i]-48;

            a=a*10+b;

            

        }

        i++;

    }

    if(t==1)

    return -a;

    else

    return a;

}

int main()

{

    char str[20];

      printf("Enter a numeric string : ");

    scanf("%s", str);

    int res = my_atoi(str);

    printf("String to integer is %d\n", res);

}

---------------------------------------------------------------------------------------------------------------

 

Comments

Popular Posts

🌙