- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to check April fool news is fake or real
Suppose we have a string S with n characters. As it's the first of April, Amal is suspecting that the news she reads today are fake, and he does not want to look silly in front of all the contestants. He knows that a news is fake if it contains "fool" as a subsequence. We have to check whether the news is really fake or not.
Problem Category
To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.
https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
https://www.tutorialspoint.com/cprogramming/c_strings.htm
So, if the input of our problem is like S = "domibecomesfool", then the output will be True, as the news is fake.
Steps
To solve this, we will follow these steps −
flag := 0 T := "fool" for initialize i := 0, when S[i] is non-zero and T[flag] is non-zero, update (increase i by 1), do: if S[i] is same as T[flag], then: (increase flag by 1) if flag is same as 4, then: return true return false
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(string S){ int flag = 0; string T = "fool"; for (int i = 0; S[i] && T[flag]; i++){ if (S[i] == T[flag]) flag++; } if (flag == 4) return true; return false; } int main(){ string S = "domibecfooomesl"; cout << solve(S) << endl; }
Input
"domibecfooomesl"
Output
1