Advertisement
Vxzyd

tablice charów T12 Ćwiczenia - łańcuchy znaków

Jan 10th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1.      #include <iostream>
  2.         #include <string.h>
  3.  
  4.         using namespace std;
  5.  
  6.         int moje_strlen( char* lancuch )
  7.         {
  8.             int i=0;
  9.             for(;;)
  10.             {
  11.                 if (lancuch [i]=='\0')
  12.                 {
  13.                     break;
  14.                 }
  15.                 i++;
  16.             }
  17.             return i;
  18.         }
  19.  
  20.         char* moje_strupr(char* lancuch )
  21.         {
  22.              int i=0;
  23.              int kod;
  24.             for(;;)
  25.             {
  26.                 if (lancuch [i]=='\0')
  27.                 {
  28.                     break;
  29.                 }
  30.             kod=static_cast <int> ( lancuch [i] );
  31.             if (kod>=97 && kod <=122)
  32.             {
  33.                 lancuch[i]=kod-32;
  34.             }
  35.             i++;
  36.         }
  37.         return (lancuch);
  38.         }
  39.  
  40.         char* moje_strcpy( char* test2, char* test1)
  41.         {
  42.             test2=test1;
  43.             return test2;
  44.         }
  45.         void str_zamien(char* tekst, char* stary_wzorzec, char* nowy_wzorzec)
  46.         {
  47.             char* wsk = tekst;
  48.             int dlugosc_starego = strlen( stary_wzorzec );
  49.             int dlugosc_nowego = strlen( nowy_wzorzec );
  50.             do
  51.             {
  52.                 wsk = strstr( tekst, stary_wzorzec );
  53.                 if( wsk ) // if( wsk != null )
  54.                 {
  55.                     // ewentualne zsunięcie lub rozsunięcie tekstu
  56.                     memmove(   wsk + dlugosc_nowego ,
  57.                     wsk + dlugosc_starego ,
  58.                     strlen( wsk + dlugosc_starego ) +1 );
  59.                     // wpisanie nowego wzorca w przygotowane miejsce
  60.                     memcpy( wsk, nowy_wzorzec, dlugosc_nowego);
  61.                 }
  62.             } while( wsk );
  63.         }
  64.  
  65.         int main()
  66.         {
  67.             int x,y;
  68.             cout << "Wprowadz wielkosc test1"<< endl;
  69.             cin >> x;
  70.             cout << "Wprowadz wielkosc test2"<< endl;
  71.             cin >> y;
  72.             char test1[x];
  73.             char test2[y];
  74.  
  75.             for (int i=0; i<x; i++)
  76.             {
  77.                 cin >> test1 [i];
  78.             }
  79.             test1[x]= NULL ;
  80.             cout << test1 << endl;
  81.  
  82.             for (int i=0; i<y; i++)
  83.             {
  84.                 cin >> test2 [i];
  85.             }
  86.             test2[y]= NULL ;
  87.             cout << test2 << endl;
  88.  
  89.             cout << moje_strlen( test1 )<< endl;
  90.             cout << moje_strlen( test2 )<< endl;
  91.             cout << moje_strupr( test1 ) << endl;
  92.             cout << moje_strupr( test2 ) << endl;
  93.             cout << moje_strcpy( test2, test1) << endl;
  94.             cout << moje_strcpy( test1, test2) << endl;
  95.  
  96.  
  97.             char tekst[]= "ala dostala kota";
  98.             char staryw[]= "kota";
  99.             char nowyw[]= "psa";
  100.  
  101.             cout << "tekst: " << tekst << endl;
  102.             cout << "staryw: " << staryw << endl;
  103.             cout << "nowyw: " << nowyw << endl;
  104.             str_zamien( tekst, staryw, nowyw);
  105.             cout << "tekst: " << tekst << endl;
  106.             return 0;
  107.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement