Skill Rack
Daily Challenge Solution
Date : 28/03/2019
ProgramID- 8451
Counter from String
The program must accept a string S as the input. The program must maintain a counter which starts from 0 based on the characters in the string. The program must traverse the string and manipulate the counter based on the following conditions.
If the character is I (i.e. uppercase i) the program must increment the counter by 1.
If the character is D the program must decrement the counter by 1.
If the character is R the program must Reset the counter to 0.
Finally, the program must print the value of the counter.
Boundary Condition(s):
1 <= Length of String <= 1000
Input Format:
The first line contains S.
Output Format:
The first line contains the final counter value.
Example Input/Output 1:
Input:
DRIDD
Output:
-1
Explanation:
Counter starts from 0.
For the character D the counter is decremented by 1. So the counter becomes -1.
For the character R the counter is reset to 0.
For the character I the counter is incremented by 1. So the counter becomes 1.
For the character D the counter is decremented by 1. So the counter becomes 0.
For the character D the counter is decremented by 1. So the counter becomes -1.
Example Input/Output 2:
Input:
RDRIDRDIII
Output:
2
Solution:
------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include <stdlib.h>
int main()
{
char a[1000];
int i,n=0;
scanf("%s",a);
for(i=0;a[i]!=NULL;i++)
{
if(a[i]=='I')
{
n++;
}
else if(a[i]=='D')
{
n--;
}
else if(a[i]=='R')
{
n=0;
}
}
printf("%d",n);
return 0;
}
#include <stdlib.h>
int main()
{
char a[1000];
int i,n=0;
scanf("%s",a);
for(i=0;a[i]!=NULL;i++)
{
if(a[i]=='I')
{
n++;
}
else if(a[i]=='D')
{
n--;
}
else if(a[i]=='R')
{
n=0;
}
}
printf("%d",n);
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment