Advertisement
thienlang

split

Oct 13th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <string.h>
  6.  
  7. using namespace std;
  8.  
  9. char* split( char* a, int start, int end)
  10. {
  11.     char words[12];
  12.     for ( int i = start; i < end; i++)
  13.     {
  14.         words[i-start] = a[i];
  15.     }
  16.     words[end - start] = 0;
  17.     return words;
  18. }
  19.  
  20. //a là chuỗi cần cắt, limit là ký tự phân chia. vd: cắt từ thì limit là ' '
  21. void reWordsString( char *a, char limit)
  22. {
  23.     int length = strlen(a);
  24.     int start = 0;
  25.     int end = length;
  26.     char words[12];
  27.     for ( int i = length; i >= -1; i--)
  28.     {
  29.         if ( a[i] == limit || i == -1)
  30.         {
  31.             start = i + 1;
  32.             strcpy(words, split(a, start, end));
  33.             cout << words << ' ';
  34.             end = i;
  35.         }
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.     char *a = "hoc lai tu can ban nhe";
  42.     reWordsString(a, ' ');
  43.    
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement