Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *wybierz_od_konca(char *tekst, int k);
  6. char *reverseArray(char *arr);
  7.  
  8. int main(void)
  9. {
  10.     char lancuch[100];
  11.     char *utworzonyLancuch = NULL;
  12.     char *prawdziwy = NULL;
  13.     int numer;
  14.  
  15.     printf("Dawaj jakiego stringa\n>");
  16.     fflush(stdin);
  17.     scanf("%99[^\n]s", lancuch);
  18.  
  19.     printf("Dawaj jakis numer\n>");
  20.     fflush(stdin);
  21.     scanf("%d", &numer);
  22.  
  23.     printf("Utworzono lancuch: %s\n", lancuch);
  24.  
  25.     utworzonyLancuch = wybierz_od_konca(lancuch, numer);
  26.     printf("Nowy lancuch: %s", utworzonyLancuch);
  27. }
  28.  
  29.  
  30. char *wybierz_od_konca(char *tekst, int k)
  31. {
  32.     char *wynik = NULL;
  33.     int i;
  34.     int iloscZnakow = 0;
  35.  
  36.     for (i = strlen(tekst) - 1; i >= 0; i = i - k, iloscZnakow++)
  37.     {
  38.         if (wynik == NULL)
  39.         {
  40.             wynik = malloc(sizeof(char));
  41.         }
  42.         else
  43.         {
  44.             wynik = realloc(wynik, sizeof(char) * iloscZnakow);
  45.         }
  46.  
  47.         wynik[iloscZnakow] = tekst[i];
  48.     }
  49.  
  50.     return reverseArray(wynik);
  51. }
  52.  
  53. char *reverseArray(char *arr) {
  54.     char *result = NULL;
  55.     int counter = 0;
  56.     int i;
  57.  
  58.     for (i = strlen(arr) - 1; i >= 0; i--, counter++)
  59.     {
  60.         if (result == NULL)
  61.         {
  62.             result = malloc(sizeof(char));
  63.         }
  64.         else
  65.         {
  66.             result = realloc(result, sizeof(char) * counter);
  67.         }
  68.  
  69.         result[counter] = arr[i];
  70.     }
  71.  
  72.     return result;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement