Imprime todas as palavras engraçadas em uma string
Recebemos uma sentença. Nossa tarefa é imprimir todas as palavras / strings engraçadas nessa frase.
O que é uma palavra engraçada?
Inverta a string fornecida. Repita cada caractere dessa string, compare a diferença absoluta nos valores ASCII dos caracteres nas posições 0 e 1, 1 e 2, 2 e 3 e assim por diante até o final. Se a lista de diferenças absolutas é a mesma para ambas as strings, elas são engraçadas do contrário, não.
Exemplos:
Input : HKMNPS Output : Yes Let r be the reverse of original string s s = "HKMNPS" r = "SPNMKH" |H-K| = 3 = |S-P| |K-M| = 2 = |P-N| |M-N| = 1 = |N-M| |N-P| = 2 = |M-K| |P-S| = 3 = |K-H| Since each comparison is equal so given string is funny Input : bdwy Output : No
NOTA: Cada string de palíndromo é uma string engraçada, mas não vice-versa.
A ideia é dividir a string em palavras. Para cada palavra, atravesse-a de ambas as extremidades e compare as diferenças entre os caracteres adjacentes.
// C++ pprogram to print all
// funny words in a string
#include <bits/stdc++.h>
using namespace std;
bool checkFunny(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)
{
// to extract last word of sentence
str += " ";
// to word stores each word of sentence
string word = "";
for (int i = 0; i < str.length(); i++)
{
char ch = str[i];
// extracting each wor
if (ch != ' ')
word += ch;
else
{
if (checkFunny(word))
cout << word << endl;
word = "";
}
}
}
// Driver Code
int main()
{
printFunnyWords("Miss Arora teaches us malayalam bdwy ");
return 0;
}
// This code is contributed by
// sanjeev2552
class Funny {
static boolean checkFunny(String word)
{
int i = 1;
int j = word.length() - 2;
word = word.toLowerCase();
while (i <= j) {
if ((Math.abs(word.charAt(i) - word.charAt(i - 1))) !=
Math.abs((word.charAt(j) - word.charAt(j + 1))))
return false;
i++;
j--;
}
return true;
}
static void printFunnyWords(String str)
{
// to extract last word of sentence
str = str + " ";
// to word stores each word of sentence
String word = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// extracting each word
if (ch != ' ')
word = word + ch;
else {
if (Funny.checkFunny(word))
System.out.println(word);
word = "";
}
}
}
public static void main(String[] args)
{
Funny.printFunnyWords("Miss Arora teaches us malayalam bdwy ");
}
}
# Python program to print all funny words in a string
def checkFunny(word):
i = 1
j = len(word) - 2
word = word.lower()
while (i <= j):
if ((abs(ord(word[i]) - ord(word[i - 1])))
!= abs((ord(word[j]) - ord(word[j + 1])))):
return False
i = i + 1
j = j - 1
return True
def printFunnyWords(str):
# to extract last word of sentence
str = str + " "
# to word stores each word of sentence
word = ""
i = 0
for i in range(len(str)):
ch = str[i]
# extracting each word
if (ch != ' '):
word = word + ch
else:
if (checkFunny(word)):
print (word)
word = ""
# Driver code
printFunnyWords("Miss Arora teaches us malayalam bdwy ")
# This code is contributed by Prateek Bajaj
// C# program to print funny string
using System;
class GFG
{
public static bool checkFunny(string word)
{
int i = 1;
int j = word.Length - 2;
word = word.ToLower();
while (i <= j)
{
if ((Math.Abs(word[i] -
word[i - 1])) != Math.Abs((word[j] -
word[j + 1])))
{
return false;
}
i++;
j--;
}
return true;
}
public static void printFunnyWords(string str)
{
// to extract last word of sentence
str = str + " ";
// to word stores each word of sentence
string word = "";
for (int i = 0; i < str.Length; i++)
{
char ch = str[i];
// extracting each word
if (ch != ' ')
{
word = word + ch;
}
else
{
if (GFG.checkFunny(word))
{
Console.WriteLine(word);
}
word = "";
}
}
}
// Driver Code
public static void Main(string[] args)
{
GFG.printFunnyWords("Miss Arora teaches us " +
"malayalam bdwy ");
}
}
// This code is contributed by Shrikant13
<script>
// Javascript pprogram to print all
// funny words in a string
function checkFunny(word)
{
var i = 1;
var j = word.length - 2;
word= (word.toLowerCase());
while (i <= j)
{
if (Math.abs(word[i].charCodeAt(0) -
word[i - 1].charCodeAt(0)) !=
Math.abs(word[j].charCodeAt(0) -
word[j + 1].charCodeAt(0)))
return false;
i++;
j--;
}
return true;
}
function printFunnyWords(str)
{
// to extract last word of sentence
str += " ";
// to word stores each word of sentence
var word = "";
for (var i = 0; i < str.length; i++)
{
var ch = str[i];
// extracting each wor
if (ch != ' ')
word += ch;
else
{
if (checkFunny(word))
document.write( word + "<br>");
word = "";
}
}
}
// Driver Code
printFunnyWords("Miss Arora teaches us malayalam bdwy ");
</script>
Arora nós malayalam bdwy
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="374" src="https://www.youtube.com/embed/f0GkhCOX5eY?feature=oembed" width="665"></iframe>
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva