TCS NQT Coding Question 2022
TCS NQT Coding Question 2022
SOLVING USING BASIC C PROGRAMMING
QUESTION:
Problem Statement – Given a string S(input consisting) of ‘*’ and ‘#’. The length of the string is variable. The task is to find the minimum number of ‘*’ or ‘#’ to make it a valid string. The string is considered valid if the number of ‘*’ and ‘#’ are equal. The ‘*’ and ‘#’ can be at any position in the string.
Note : The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in the input string.
(*>#): positive integer
(#>*): negative integer
(#=*): 0
Example 1:
Input 1:
###*** -> Value of S
Output :
0 → number of * and # are equal
CODE:
#include<stdio.h>
#include<string.h>
int main()
{
int count1=0, count2=0, i=0;
char S[100];
scanf("%[^\n]",S);
for(i=0;S[i]!=0;i++)
{
if(S[i]=='*')
count1++;
else
count2++;
}
if(count1==count2)
printf("0");
else if(count1<count2)
printf("negative integer");
else
printf("positive integers");
}
IMAGE:
Comments
Post a Comment