
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- How FullFact is Planning to end Circulation of Fake news on Internet
- Journalists who report Fake news will lose their accreditation !!
- Difference between Real and Fake Diamonds
- Bad News vs Serious News
- C++ Program to Check Whether Number is Even or Odd
- C Program to check if matrix is singular or not
- C# program to check if string is panagram or not
- C++ Program to check string is strictly alphabetical or not
- Swift program to check if string is pangram or not
- Golang Program to check a directory is exist or not
- C program to Check Whether a Number is Positive or Negative or Zero?
- Fetching top news using news API in Python
- Python program to check if a string is palindrome or not
- Java Program to check if a string is empty or not
- C++ Program to Check Whether a Number is Prime or Not
