fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. // Functie pentru a verifica daca un caracter este litera
  6. bool esteLitera(char c) {
  7. return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
  8. }
  9.  
  10. // Functie pentru a numara caracterele alfabetice dintr-un cuvant
  11. int nrChar(const char* cuvant) {
  12. int count = 0;
  13. for (int i = 0; cuvant[i] != '\0'; i++) {
  14. if (esteLitera(cuvant[i])) {
  15. count++;
  16. }
  17. }
  18. return count;
  19. }
  20.  
  21. void inter(char c[], char rez[]) {
  22. int n = strlen(c);
  23. int litere = nrChar(c);
  24. strcpy(rez, c);
  25. int mijloc = litere / 2;
  26. int leftIndex = 0, rightIndex = 0;
  27. for (int i = 0; i < n; ++i) {
  28. if (esteLitera(c[i])) {
  29. if (leftIndex < mijloc) {
  30. rez[mijloc + rightIndex] = c[i];
  31. rightIndex++;
  32. } else {
  33. rez[leftIndex - mijloc] = c[i];
  34. leftIndex++;
  35. }
  36. }
  37. }
  38. if (litere % 2 != 0) {
  39. rez[mijloc] = c[mijloc];
  40. }
  41. }
  42.  
  43. int main() {
  44. char car[1000], rezCuv[1000];
  45. cout << "Introduceti textul (se opreste cu linie goala):" << endl;
  46.  
  47. while (cin.getline(car, 1000) && strlen(car) > 0) {
  48. char* cuvant = strtok(car, " ");
  49. while (cuvant != nullptr) {
  50. inter(cuvant, rezCuv); // Procesam fiecare cuvant
  51. cout << rezCuv << " ";
  52. cuvant = strtok(nullptr, " ");
  53. }
  54. cout << endl;
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5276KB
stdin
Hello world
stdout
Introduceti textul (se opreste cu linie goala):
Helello wororld