Professional Documents
Culture Documents
Day 8 Challenge Yourself
Day 8 Challenge Yourself
Day 8 Challenge Yourself
PSCPP_String_CY
Attempt : 1
Total Mark : 50
Marks Obtained : 38
Section 1 : Coding
You are given whether he ate a cookie or drank fresh juice in each of the N
minutes. Your task is to check if Casper followed his parents' instructions.
That is, you need to verify whether after each eaten cookie he drinks fresh
juice in the next minute.
Answer
// You are using GCC
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n;
cin>>n;
string a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int c=0;
for(int i=0;i<n;i++)
{
if(a[i]=="cookie"&&a[i+1]!="juice")
{
c++;
break;
}
}
if(c)
{
cout<<"No"<<endl;
}
else
{
cout<<"Yes"<<endl;
}
}
2. Wildcard Matching:
Answer
// You are using GCC
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string x,y;
cin>>x>>y;
//if(x.length() != y.length())
/* {
cout<<"No"<<endl;
return 0;
}*/
for(int i=0;i<x.length();i++)
{
if(x[i]!=y[i]&& x[i] !='?'&&y[i] !='?')
{
cout<<"No"<<endl;
return 0;
}
}
cout<<"Yes"<<endl;
return 0;
}
3. Write a program to remove the characters which have odd index values
of a given string
Answer
// You are using GCC
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string a;
cin>>a;string result;
for(int i=0;i<a.length();i+=2)
{
result+=a[i];
}
cout<<result;
}
4. Write a program to get a string from user and remove all characters in
that string except alphabets.
Answer
// You are using GCC
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string a,temp="";
cin>>a;
int n=a.length();
for(int i=0;i<n;i++)
{
if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z'))
{
temp=temp+a[i];
}
}
a=temp;
cout<<a;
}
5. Toggle each character in a string. (From lower case to upper case and
vice versa). If any special characters are found, leave it as it is.
Answer
// You are using GCC
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
int main()
{
string input;
getline(cin,input);
for(char &c:input)
{
if(islower(c))
c=toupper(c);
else if(isupper(c))
c=tolower(c);
}
cout<<input<<endl;
return 0;
}