Advertisement
palenda21

Lab7B'

Dec 3rd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. //удалить предпоследнее слово в строке
  2. #include <string.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     char str[100];
  10.     char buf[100];
  11.     cout << "Vvedite stroku: ";
  12.     cin.getline(str, 100);
  13.     int j = 0, i;
  14.     for (i = strlen(str)-1; i >= 0; i--)
  15.     {
  16.         if ((j != 0) && (str[i] == ' '))
  17.             break;
  18.         buf[j++] = str[i];
  19.         buf[j] = 0;
  20.     }
  21.     int sled = 0;
  22.     for (; i >= 0; i--)
  23.     {
  24.         if (str[i] != ' ')
  25.             sled = 1;
  26.         if ((str[i] == ' ') && (sled))
  27.             break;
  28.     }
  29.     i++;
  30.      do
  31.      {
  32.          str[i++] = buf[--j];
  33.      } while (buf[j]);
  34.     cout << str << endl;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement