Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Napisz program który wczytuje znaki z klawiatury i zapisuje w dynamicznej tablicy. Koniec wczytywania
- //jest sygnalizowany kropką. Po zakończeniu wczytywania program wypisuje wczytany napis. Rozmiar
- //tablicy powinien na początku wynosić 10 znaków i być zwiększany po 10 znaków w miarę potrzeby.
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h> // memcpy
- #define N 10
- char* alloc(char* tab, int n)
- {
- printf("%s%d\n", "Alokacja rozm:", n);
- char* new_t = NULL;
- new_t = calloc( n+N, sizeof(*tab) );
- memcpy(new_t, tab, n * sizeof(*tab) );
- return new_t;
- }
- int main()
- {
- printf("%s", "Wprowadzaj znaki. Kropka '.' konczy program\n");
- char *tab = calloc( N+1, sizeof( *tab ) );
- int c = getch();
- do{
- if( (strlen(tab)+1) % 10 == 0 )
- tab = alloc( tab, strlen(tab)+1); // +1 bo znak '/0'
- tab[ strlen(tab) ] = c;
- printf("rozm: %d (%c)\n", strlen(tab), tab[strlen(tab)-1] );
- c = getch();
- }while( c != '.' );
- if(tab != NULL)
- {
- for(int i=0; i<strlen(tab); i++)
- printf("%c, ", tab[i]);
- free(tab);
- tab = NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement