Commit 6096dd89 by Patryk Czarnik

Zajęcia 23.11 - palindorm

parent c672eb5c
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 20;
int main()
{
srand(time(nullptr));
// Tworzymy tablicę 20 liczb z zakresu od 0 do 99.
int t[SIZE];
for(int i = 0; i < SIZE; i++) {
t[i] = rand() % 100;
}
// wypisanie tablicy
for(int i = 0; i < SIZE; i++) {
cout << t[i] << ", ";
}
cout << endl;
// Zadanie: znajdz w tablicy najmniejsza i najwieksza liczbe i je wypisz.
int max = ???????;
for(int i = 0; i < SIZE; i++) {
if(t[i] > max) {
max = t[i];
}
}
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main()
{
cout << "Napisz swój tekst:\n";
string napis;
getline(cin, napis);
cout << "Cały tekst: " << napis << endl;
cout << "Długość tekstu: " << napis.length() << endl;
cout << "Pierwsza litera: " << napis[0] << endl;
cout << "Środkowy znak: " << napis[napis.length() / 2] << endl;
cout << "Ostatnia litera: " << napis[napis.length() - 1] << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << "Napisz swój tekst:\n";
string napis;
getline(cin, napis);
bool czy_jest_ok = true;
for(int i = 0; i < napis.length(); i++) {
// cout << napis[i] << " vs " << napis[napis.length()-(i+1)] << endl;
if(napis[i] != napis[napis.length()-(i+1)]) {
// jeśli gdzieś znak z początku nie zgadza się ze znakiem z końca, tzn że napis nie jest palidromem
czy_jest_ok = false;
}
}
if(czy_jest_ok) {
cout << "Napis jest palindromem\n";
} else {
cout << "Napis nie jest palindromem, bo odwrotnie wygląda tak:\n";
for(int i = napis.length()-1; i >= 0; i--) {
cout << napis[i];
}
cout << endl;
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment