Jump to content
hitme

Problema C_Siruri de Caractere

Recommended Posts

Salut, cum pot inlocui intr-un sir de caractere, un cuvant cu un altul. Functia strstr, gaseste doar prima apartie a cuvantului si returneaza un pointer. M-am gandit sa formez un alt sir, in care sa pun tot ce am in sir1, pana la cuvantul gasit, apoi sa pun cuvantul de inlocuit si sa continui asa pana la sfarsitul sirului.

Am incercat sa scriu asta in cod, doar ca nu-s deloc mandru de ce a iesit :))

Cineva mai ager ca mine si care are timp sa scrie putin cod? Thx

Share this post


Link to post
Share on other sites
Salut, cum pot inlocui intr-un sir de caractere, un cuvant cu un altul. Functia strstr, gaseste doar prima apartie a cuvantului si returneaza un pointer. M-am gandit sa formez un alt sir, in care sa pun tot ce am in sir1, pana la cuvantul gasit, apoi sa pun cuvantul de inlocuit si sa continui asa pana la sfarsitul sirului.

Am incercat sa scriu asta in cod, doar ca nu-s deloc mandru de ce a iesit :))

Cineva mai ager ca mine si care are timp sa scrie putin cod? Thx


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

int main()
{
string s("Your homework is bad. Really bad.");

while (s.find("bad") != string::npos)
s.replace(s.find("bad"), 3, "good");

cout << s << endl;

return 0;
}

Mai cauta pe stackoverflow. Gasesti sigur si in C, daca vrei neaparat.

Share this post


Link to post
Share on other sites

Ca sa nu deschid alt thread va rog sa imi dati si mie niste indicii in urmatorul exercitiu .

"Write a program using vectors and iterators that allow a user to maintain a list of his / her favourite games. The program should allow the user to list all games titles , add a game title and remove a game title .

Repet , vreau doar niste indicii .

Share this post


Link to post
Share on other sites


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main()
{
int choice;
vector<string> games;

cout << "1. Listeaza jocurile\n";
cout << "2. Adauga jocuri\n";
cout << "3. Sterge jocuri\n";
cout << "4. Iesi\n";

cout << "Alege o optiune: ";
cin >> choice;

while(choice == 1 || choice == 2 || choice == 3)
{
switch(choice)
{
case 1:

for(int i = 0; i < games.size(); i++)
{
cout << games[i] << endl;
cout << "Alege o optiune: ";
cin >> choice;
}
break;

case 2:
{
cout << "Numele jocului de adaugat: ";
string game;
cin >> game;
games.push_back(game);
cout << "Lista noua:\n";

for(int i = 0; i < games.size(); i++)
{
cout << games[i] << endl;
cout << "Alege o optiune: ";
cin >> choice;
}
}
break;

case 3:
{
cout << "Lista:\n";
for(int i = 0; i < games.size(); i++)
{
cout << (i + ". " + games[i]) << endl;
cout << "Introdu numarul jocului pe care vrei sa il stergi: ";
int remove;
cin >> remove;
games.erase((games.begin() + remove));
cout << "Lista noua::\n";

for(int i = 0; i < games.size(); i++)
{
cout << games[i] << endl;
cout << "Alege o optiunr: ";
cin >> choice;
}
}
}
break;
}
}
cout << "Multumesc pentru ca ai folosit programul";
return 0;
}

E ok indiciul asta ? :D

Edited by MrGrj

Share this post


Link to post
Share on other sites

Pai ala e doar un model. Tu poti sa faci altfel.

Adica poti incerca sa gasesti alta modalitate ( asa o sa devina mai challenging)

Bafta sal :D

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...