0% found this document useful (0 votes)
118 views5 pages

c1 Programy

Uploaded by

katarzynaprze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views5 pages

c1 Programy

Uploaded by

katarzynaprze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Zadanie 1.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
s1 = "Stosujemy dane";
s2 = "tekstowe w C++";
cout << s1 + " " + s2;
return 0;
}

Zadanie 2.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string wyraz;
int dlugosc;
cout << "Podaj wyraz: ";
cin >> wyraz;
dlugosc = wyraz.length();
for(int i = 0; i < dlugosc; i++)
if( wyraz[i] == 'a' )
wyraz[i] = 'b';
cout << "Wyraz po przetworzeniu: " << wyraz << endl;
return 0;
}

Zadanie3.
#include <iostream>
#include <string>
using namespace std;

int main()
{
string wyraz;
int dlugosc;
cout << "Podaj wyraz: ";
cin >> wyraz;
dlugosc = wyraz.length();
cout << "Pierwsza litera: " << wyraz[0] << endl;
cout << "Ostatnia litera: " << wyraz[dlugosc - 1] << endl;
return 0;
}

Zadanie 4.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string wyraz;
int dlugosc, litera1, i;
cout << "Podaj wyraz: ";
cin >> wyraz;
dlugosc = wyraz.length();
litera1 = wyraz[0];
if(wyraz[0] != wyraz[dlugosc - 1])
{
wyraz[0] = wyraz[dlugosc-1];
wyraz[dlugosc-1] = litera1;
cout << wyraz;
}
else
cout << "Pierwsza i ostatnia litera sa takie same.";
return 0;
}

Zadanie
#include <iostream>
#include <string>
using namespace std;
int main()
{
string wyraz;
int dlugosc;
cout << "Podaj wyraz: ";
cin >> wyraz;
dlugosc = wyraz.length();
int i = 1;
while (i < dlugosc)
{
wyraz[i] = 'x';
i = i + 2;
}
cout << "Wyraz po zamianie co drugiej litery na x: " << wyraz << endl;
return 0;
}
Zadanie.
#include <iostream>
#include <string>
using namespace std;
bool teksty_sa_identyczne(string t1, string t2)
{
int i, dl1, dl2;
i = 0;
dl1 = t1.length();
dl2 = t2.length();
while(i < dl1 && i < dl2 && t1[i] == t2[i])
i++;
return i == dl1 && i == dl2;
}

int main ()
{
string tekst1, tekst2;
cout << "Wprowadz pierwszy tekst: ";
cin >> tekst1;
cout << "Wprowadz drugi tekst: ";
cin >> tekst2;
if(teksty_sa_identyczne(tekst1, tekst2))
cout << "Teksty sa takie same";
else
cout << "Teksty sa rozne";
return 0;
}
Zadanie.
#include <iostream>
#include <string>
using namespace std;
int zlicz_a(string tekst)
{
int liczba_znakow = 0;
for(int i = 0; i < tekst.length(); i++)
if(tekst[i] == 'a')
liczba_znakow++;
return liczba_znakow;
}
int main()
{
string napis;
char znak;
cout << "Podaj napis: ";
cin >> napis;
cout << "Liczba znakow " << "\"" << "a" << "\"" << " w napisie " << napis << " wynosi: " <<
zlicz_a(napis) << endl;
return 0;
}
#include <iostream>
#include <string>

using namespace std;

int zlicz_znak(string tekst)


{
int liczba_znakow = 0;

for(int i = 0; i < tekst.length(); i++)


if(tekst[i] == 'a')
liczba_znakow++;
return liczba_znakow;
}

int main()
{
string napis;
cout << "Podaj napis: ";
getline(cin, napis);
cout << "Liczba znakow " << "a" << " w napisie " << napis << " wynosi: " << zlicz_znak(napis) <<
endl;
return 0;
}

Zadanie
clude <iostream>
#include <string>
using namespace std;
int zlicz_znak(string tekst, char z)
{
int liczba_znakow = 0;

for(int i = 0; i < tekst.length(); i++)


if(tekst[i] == z)
liczba_znakow++;
return liczba_znakow;
}
int main()
{
string napis;
char znak;

cout<<"**********************************"<<endl;
cout<<"MENU PROGRAMU"<<endl;
cout<<"a. Zliczanie wszystkich liter a"<<endl;
cout<<"b. Zliczanie wszystkich liter b"<<endl;
cout<<"c. Zliczanie wszystkich liter b"<<endl;
cout<<"**********************************"<<endl;

cout << "Podaj napis: ";


getline(cin, napis);
cout << "Podaj znak opcji: ";
cin >> znak;

switch(znak)
{
case 'a':
cout << "Liczba znakow " << znak << " w napisie " << napis << " wynosi: " <<
zlicz_znak(napis, znak) << endl;
break;

case 'b':
cout << "Liczba znakow " << znak << " w napisie " << napis << " wynosi: " <<
zlicz_znak(napis, znak) << endl;
break;

case 'c':
cout << "Liczba znakow " << znak << " w napisie " << napis << " wynosi: " <<
zlicz_znak(napis, znak) << endl;
break;

return 0;
}

Zadanie
#include <iostream>
using namespace std;
int znajdz_tekst(string tekst, string wzorzec, int pozycja_startowa)
{
int dl1 = tekst.length(); // Użycie metody standardowej length()do wyznaczenia
długości tekstu...
int dl2 = wzorzec.length(); // ... i wzorca

for(int i = pozycja_startowa; i < dl1-dl2+1; i++) // Dla każdego znaku tekstu


{
int j;
for(j = 0; j < dl2; j++) // Dla każdego
znaku wzorca
if(tekst[i+j] != wzorzec[j]) // Rozbieżność
break; // Przerywamy pętlę wewnętrzną
if(j == dl2) // Porównano z powodzeniem wszystkie znaki wzorca
return i; // Zwracamy aktualną pozycję w tekście
}
return -1; // Nie znaleziono
}

int main()
{
// Wyszukiwanie wszystkich wystąpień
string przeszukiwany_tekst, tekst_do_wyszukania;

cout << "Wprowadz tekst do przeszukania: ";


cin >> przeszukiwany_tekst;
cout << "Wprowadz tekst do wyszukania: ";
cin >> tekst_do_wyszukania;

int pozycja = 0;
do
{
pozycja = znajdz_tekst(przeszukiwany_tekst, tekst_do_wyszukania, pozycja);
if(pozycja>=0)
{
cout << "Znaleziono: | " << przeszukiwany_tekst << " | " << tekst_do_wyszukania << " | " << pozycja <<
" | " << endl;
pozycja++; // Następne wyszukiwanie zaczyna się od kolejnego znaku po znalezionym fragmencie
}
} while(pozycja>=0);
return 0;
}

You might also like