Advertisement
TShiva

Strings_arrays

Dec 8th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. char* StringCopy(char* s)
  8. {
  9.     char* Str = 0;
  10.     int len = 0;
  11.     for (int i = 0; s[i]; i++){
  12.         len++;
  13.     }
  14.  
  15.     len++;
  16.     Str = new char [len];
  17.     for (int i = 0; i<len; i++)
  18.         Str[i] = s[i];
  19.     return Str;
  20. };
  21.  
  22.  
  23.  
  24.  
  25.  
  26. char* StringRename(char* s)
  27. {
  28.     char* Str = 0;
  29.     int len =0, counter1 = 0, counter2 = 0, counter3 = 0;
  30.     for (int i=0;s[i];i++)
  31.     {  
  32.         if (s[i]=='1')
  33.             counter1++;
  34.         if (s[i]=='2')
  35.             counter2++;
  36.         if (s[i]=='3')
  37.             counter3++;
  38.     }
  39.     int lenght=strlen(s)+counter1*2+counter2*2+counter3*4;
  40.  
  41.     int j = 0;
  42.     int lenString = strlen(s);
  43.     Str = new char [lenght+1];
  44.     for (int i = 0; i<lenString; i++)
  45.     {
  46.         switch (s[i])
  47.         {
  48.         case '1':
  49.             Str[j] = 'O';
  50.             Str[j+1] = 'n';
  51.             Str[j+2] = 'e';
  52.             j+=2;
  53.             break;
  54.         case '2':
  55.             Str[j] = 'T';
  56.             Str[j+1] = 'w';
  57.             Str[j+2] = 'o';
  58.             j+=2;
  59.             break;
  60.         case '3':
  61.             Str[j] = 'T';
  62.             Str[j+1] = 'h';
  63.             Str[j+2] = 'r';
  64.             Str[j+3] = 'e';
  65.             Str[j+4] = 'e';
  66.             j+=4;
  67.             break;
  68.         default:
  69.             Str[j] = s[i];
  70.             break;
  71.         }
  72.         j++;
  73.     }
  74.     Str[lenght]=0;
  75.     return Str;
  76. }
  77.  
  78.  
  79. bool SearchWord(char* s,int& bLet,int& FirstIndex,int& LastIndex)
  80. {
  81.    
  82.     for (;s[bLet]; bLet++)
  83.     {
  84.         if (!(s[bLet]==' '))
  85.         {
  86.             FirstIndex=bLet;
  87.             break;
  88.         }
  89.     }
  90.  
  91.     if (!s[bLet]){  return false;}
  92.     for (;s[bLet];bLet++)
  93.     {
  94.         if (s[bLet]==' ')
  95.             break;
  96.     }
  97. LastIndex=bLet;
  98. return true;
  99. }
  100.  
  101. char* WordsSwap(char* s)
  102. {
  103.     char* Str = 0;
  104.     int length=strlen(s);
  105.     int beginLetter = 0;
  106.     int fwb = 0;
  107.     int fwe = 0;
  108.     if(!SearchWord(s,beginLetter, fwb, fwe)) {
  109.         cout << "нет первого слова";
  110.         return s;
  111.     }
  112.  
  113.     int lwb = 0;
  114.     int lwe = 0;
  115.     while (SearchWord(s,beginLetter,lwb,lwe)){}
  116.    
  117.  
  118.     if (!lwb) {return s;}
  119.  
  120. char* newStr = new char [length];
  121.  
  122.     int count=0;
  123.     int lwbcount=lwb;
  124.     int fwecount=fwe;
  125.     int lwecount=lwe;
  126.     int fwbcount=fwb;
  127.  
  128.     for (int i=0;i<fwb;i++){
  129.         newStr[count]=s[i];
  130.         count++;
  131.     }
  132.  
  133.     for (lwb;lwbcount<lwe;lwbcount++){
  134.         newStr[count]=s[lwbcount];
  135.         count++;
  136.     }
  137.  
  138.     for (fwe;fwecount<lwb;fwecount++){
  139.         newStr[count]=s[fwecount];
  140.         count++;
  141.     }
  142.  
  143.     for (fwb;fwbcount<fwe;fwbcount++){
  144.         newStr[count]=s[fwbcount];
  145.         count++;
  146.     }
  147.    
  148.    
  149.     for (lwe;lwecount<length;lwecount++){
  150.         newStr[count]=s[lwecount];
  151.         count++;
  152.     }
  153.     newStr[count]=0;
  154. return newStr;
  155.  
  156. }
  157.  
  158. void NumberOfLatinSymbolsIncluded(char* s)
  159. {
  160.     int i = 0;
  161.    
  162.     int A [26];
  163.     for(int i = 0; i<26; i++)
  164.     {
  165.         A[i]=0;
  166.     }
  167.    
  168.     while (s[i])
  169.     {   if ((s[i]>='A') && (s[i]<='Z'))
  170.         A[s[i]-'A']++;
  171.     else if ((s[i]>='a') && (s[i]<='z')
  172.         {A[s[i]-'a']++;}
  173.        
  174.         i++;
  175.  
  176.     }
  177.     for(int j = 0; j<26; j++)
  178.     {
  179.         cout << A[j] << " ";
  180.     }
  181. }
  182.  
  183. char* DeleteEachSecondSymbolInString(char *s)
  184. {
  185.     char* Str = 0;
  186.     int lenght = 0;
  187.     lenght = (strlen(s)+(strlen(s)%2==0 ? 0 : 1))/2;
  188.     Str = new char [lenght+1];
  189.     int j = 0;
  190.     for (int i = 0; i<strlen(s); i+=2)
  191.     {  
  192.         Str[j] = s[i];
  193.         j++;
  194.     }
  195.     Str[lenght] = 0;
  196.     return Str;
  197. }
  198.  
  199. char** ArrayChange(char** mas, int M)
  200. {
  201.     char** table = 0;
  202.     table = new char* [M];
  203.     for (int i = 0; i<M; i++)
  204.     {
  205.         int j = 0;
  206.         int len = strlen(mas[i]);
  207.         for (j; mas[i][j]; j++)
  208.             if (mas[i][j]==' ')
  209.                 break;
  210.         table[i] = new char [j+7];
  211.         table[i][0] = mas[i][j+1];
  212.         table[i][1] = '.';
  213.         table[i][2] = ' ';
  214.         int k ;
  215.         for (k = j+1; mas[i][j]; k++)
  216.             if (mas[i][k]==' ')
  217.                 break;
  218.         table[i][3] = mas[i][k+1];
  219.         table[i][4] = '.';
  220.         table[i][5] = ' ';
  221.         for (int q = 0; q<j; q++)
  222.             table[i][q+6] = mas[i][q];
  223.         //table[i][j+6] = ';';
  224.         table[i][j+6] = 0;
  225.  
  226.     }
  227.     return table;
  228. }
  229.  
  230. int* ArrayContainAllEvenNumbers(int* mas, int size, int &cnr)
  231. {
  232.     cnr = 0;
  233.     int *res = 0;
  234.     for (int i = 0; i<size; i++)
  235.         if (mas[i]%2==0)
  236.             cnr++;
  237.     res = new int [cnr];
  238.     int j = 0;
  239.     for (int i = 0; i<size; i++)
  240.     {
  241.         if (mas[i]%2==0)
  242.         {
  243.             res[j] = mas[i];
  244.             j++;
  245.         }
  246.     }
  247.     return res;
  248. }
  249.  
  250. // -1163005939
  251. int AllSymbolsTakenOneTime(int*p,int B)
  252. {
  253.         int length=B;
  254.         int*mas_simv=new int[length];
  255.         int symbol;
  256.         bool flag;
  257.         int j=0;
  258.        
  259.  
  260.         for (int i=0;i<length;i++)
  261.         {
  262.                 symbol=p[i];
  263.                 flag=false;
  264.                 for (int k=0; k<j;k++) {
  265.                    
  266.                         if (mas_simv[k]==symbol)
  267.                             flag=true;
  268.                 }
  269.                
  270.                 if (flag==false)
  271.                 {
  272.                         mas_simv[j]=p[i];
  273.                         j++;
  274.                 }      
  275.         }
  276.         for(int i=0;i<j;i++)
  277.                         cout<<mas_simv[i]<<" ";
  278.         delete[] mas_simv;
  279. return 0;
  280. }
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287. int main(){
  288.     char* MyString = 0;
  289.     char* NewStr = 0;
  290.     const int N= 100;
  291.     char string[N];
  292. //  cout << "Enter the string:" << endl;
  293. //  cin.getline(string, N);
  294.  
  295.     /*MyString = StringCopy(string);
  296.     cout << MyString << endl;
  297.  
  298.     NewStr = StringRename(MyString);
  299.     cout << NewStr << endl;
  300. */
  301.     //NewStr = WordsSwap(string);
  302. //  cout << NewStr << endl;
  303.  
  304. //  NumberOfLatinSymbolsIncluded(string);
  305. /*
  306. cout << DeleteEachSecondSymbolInString(string) << endl;
  307. */
  308. /*
  309.     int M = 0;
  310.     char ** mas = 0;
  311.     cout << "How many students will be?" << endl;
  312.     cin >> M;
  313.     cin.ignore(256,'\n');
  314.     cout << "Enter F.I.O of these students:" << endl;
  315.     mas = new char* [M];
  316.     for (int i = 0; i<M; i++)
  317.     {
  318.         cin.getline(string, N);
  319.         mas[i] = StringCopy(string);
  320.     }
  321.     mas = ArrayChange(mas, M);
  322.     for (int i = 0; i<M; i++)
  323.     {
  324.         cout << mas[i] << endl;
  325.     }
  326.     */
  327. /*
  328.         int size = 0, cnr=0;
  329.     cout << "How many elements will be?" << endl;
  330.     cin >> size;
  331.     int* massiv = 0;
  332.     massiv = new int [size];
  333.     cout << "Inpunt string of " << size << " numbers." << endl;
  334.     for (int i = 0; i<size; i++)
  335.         cin >> massiv[i];
  336.     int* NewMas = 0;
  337.     NewMas = ArrayContainAllEvenNumbers(massiv, size, cnr);
  338.     if (cnr==0) cout << "There are no even numbers." << endl;
  339.     else
  340.     {
  341.         cout << "Array of all even numbers: ";
  342.         for (int i = 0; i<cnr; i++)
  343.             cout << NewMas[i] << "  ";
  344.         cout << endl;
  345.     }
  346.     cnr=0;
  347. */
  348.  
  349.        int B;
  350.         cout<<"Put the number of elements"<<endl;
  351.         cin>>B;
  352.         int *p=0;
  353.         p=new int[B];
  354.         for (int i=0;i<B;i++)cin>>p[i];
  355.         AllSymbolsTakenOneTime(p,B);
  356.  
  357.  
  358.  
  359.     system("pause");
  360.  
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement