Skip to content

Instantly share code, notes, and snippets.

@cecinuga
Last active April 29, 2018 11:07
Show Gist options
  • Select an option

  • Save cecinuga/0d5b4b636ccca4a48854fbb29f954a67 to your computer and use it in GitHub Desktop.

Select an option

Save cecinuga/0d5b4b636ccca4a48854fbb29f954a67 to your computer and use it in GitHub Desktop.
Programma in c++ che restituisce informazioni sulla stringa data in input
#include <iostream>
#include <string>
using namespace std;
int n;
string frase;
void scambia(char &a, char &b){
char z;
z = a;
a = b;
b = z;
}
bool palindromo(string frase){
string frase_ = frase;
for(int i = 0; i < frase_.length(); i++)
if(frase_[i]==' ')
frase_.erase(frase_.begin()+i);
n = frase_.length()-1;
bool pali = true;
int pal = 0;
for(int i = 0; i < n/2; i++){
if(frase_[i]!=frase_[n-i])
pali=false;
break;
}
return pali;
}
void bubbleSort(int n){
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (int(frase[j]) > int(frase[j+1]))
scambia(frase[j],frase[j+1]);
}
void conteggio_lettere_parole(){
cout<<"\nConta delle lettere: \n";
int cont = 0, var = 0, spazio=0;
for(int i = 0; i < n+1; i++){
if(i>=1){
if(frase[i]!=frase[i-1]){
cout<<char(frase[i-1])<<"-"<<cont+1<<endl;
var++;
cont=0;
}
else
cont++;
}
if(char(frase[i])==char(' '))
spazio++;
}
cout<<"\nCaratteri diversi: "<<var<<endl<<"Numero parole: "<<spazio+1<<endl;
}
int main(){
getline(cin, frase);
cout<<"Palindromo:"<<palindromo(frase);
bubbleSort(n);
conteggio_lettere_parole();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment