- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Print all funny words in a string in C++
In this problem, we are given a sentence. Our task is to print all strings from the sentence that are funny words.
Funny word is a word that follows the condition - The absolute difference between adjacent characters of the string and its reverse string is equal.
|string[0] - string[1]| = |revstring[0]-revstring[1]|
Let’s take an example to understand the problem −
Input: string = ‘ABRS’ Output: Yes Explanation: Reverse string = SRBA |A-B| = 1 = |S-R| |B-R| = 16 = |R-B| |B-A| = 1 = |R-S|
To solve this problem, we have to extract each string from the given sentence. And print if the string is a funny string.
Check for funny string − For this, we will traverse the string from both ends i.e. from start and from the end. And compare the absolute difference between adjacent characters of the string and return false if the difference is not the same.
The below code will implement our logic −
Example
#include <iostream> #include<string.h> using namespace std; bool isFunny(string word){ int i = 1; int j = word.length() - 2; for (int i = 0; i < word.length(); i++) word[i] = tolower(word[i]); while (i <= j){ if (abs(word[i] - word[i - 1]) != abs(word[j] - word[j + 1])) return false; i++; j--; } return true; } void printFunnyWords(string str){ str +=" "; string word = ""; for (int i = 0; i < str.length(); i++){ char ch = str[i]; if (ch!=' ') word += ch; else{ if (isFunny(word)) cout<<word<<"\t"; word = ""; } } } int main(){ string sentence = "hello, i love malayalam langauge"; cout<<"All funny words of the string '"<<sentence<<"' are :\n"; printFunnyWords(sentence); return 0; }
Output
All funny words of the string 'hello, i love malayalam langauge' are : i malayalam
- Related Articles
- Java Program to Print all unique words of a String
- Print all possible words from phone digits in C++
- Print all words matching a pattern in CamelCase Notation Dictionary in C++
- Print all subsequences of a string in C++
- Print all palindromic partitions of a string in C++
- Print all palindrome permutations of a string in C++
- Print all words occurring in a sentence exactly K times
- Print all subsequences of a string using ArrayList in C++
- Python program to print even length words in a string
- Print all distinct characters of a string in order in C++
- Program to print all substrings of a given string in C++
- Print all subsequences of a string using Iterative Method in C++
- Print all ways to break a string in bracket form in C++
- Print all the combinations of a string in lexicographical order in C++
- Print all valid words that are possible using Characters of Array in C++

Advertisements