Programowanie Proceduralne
Transkrypt
Programowanie Proceduralne
Programowanie Proceduralne Programowanie Proceduralne Bożena Woźna-Szcześniak [email protected] Jan Długosz University, Poland Wykład 7 Programowanie Proceduralne Biblioteka string.h strlen ◮ size_t strlen(char const *s); ◮ ◮ Oblicza długość napisu wskazywanego przez s Przykładowy program: #include <stdio.h> #include <string.h> int main(void) { char* pmsg = "Linux is fun !"; printf("%d : %s\n", strlen(pmsg), pmsg); char amsg[] = "Linux is fun !"; printf("%d : %s\n", strlen(amsg), amsg); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik 14 : Linux is fun ! 14 : Linux is fun ! Programowanie Proceduralne Biblioteka string.h strcpy ◮ char* strcpy(char* t, char const* s); ◮ ◮ ◮ ◮ Kopiuje Łańccuch znaków wskazywany przez s (łacznie ˛ ze znakiem NULL) do tablicy wskazywanej przez t. Łańcuchy nie moga˛ na siebie nachodzić. Łańcuch docelowy t musi być wystarczajaco ˛ długi, aby zmieścić kopie˛ s. Funkcja strcpy zwraca wskaźnik do łańcucha docelowego t. Programowanie Proceduralne Biblioteka string.h strcpy() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char* s = "Linux is fun !"; printf("%d : %s\n", strlen(s), s); char* t = calloc(strlen(s)+1, sizeof(char)); printf("%d : %s\n", strlen(t), t); strcpy(t, s); printf("%d : %s\n", strlen(t), t); free(t); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik 14 : Linux is fun ! 0 : 14 : Linux is fun ! Programowanie Proceduralne Biblioteka string.h strncpy ◮ char* strncpy(char* t, char const* s, size_t n); ◮ ◮ ◮ ◮ Kopiuje co najwyżej n znaków z łańcucha wskazywanego przez s do tablicy wskazywanej przez t. Jeżeli wśród pierwszych n znakółw łańcucha s nie bedzie ˛ znaku NULL, łańcuch t nie bedzie ˛ zakończony znakiem NULL. Jeżeli długość s jest mniejsza niż n, to reszta tablicy t zostanie wypełniona znakiem NULL. Funkcja strncpy zwraca wskaźnik do łańcucha docelowego t Programowanie Proceduralne Biblioteka string.h strncpy() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char* s = "This program is free software !"; printf("%d : %s\n", strlen(s), s); int n = 10; char* t = calloc(n, sizeof(char)); strncpy(t, s, n - 1); t[n - 1] = ’\0’; // polecane !!! printf("%d : %s\n", strlen(t), t); strncpy(t, s, n); t[n - 1] = ’\0’; // konieczne !!! printf("%d : %s\n", strlen(t), t); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik 31 : This program is free software ! 9 : This prog 9 : This prog Programowanie Proceduralne Biblioteka string.h strncpy() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char* s = "This program is free software !"; printf("%d : %s\n", strlen(s), s); int n = 10; char* t = calloc(n, sizeof(char)); strncpy(t, s, n - 1); // t[n - 1] = ’\0’; // polecane !!! printf("%d : %s\n", strlen(t), t); strncpy(t, s, n); // t[n - 1] = ’\0’; // konieczne !!! printf("%d : %s\n", strlen(t), t); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik 31 : This program is free software ! 9 : This prog 10 : This progr Programowanie Proceduralne Biblioteka string.h strcat ◮ char* strcat(char* t, char const* s); ◮ ◮ ◮ ◮ ◮ Dołancza łańcuch znaków wskazywany przez s do łańcucha wskazywanego przez t. Łańcuchy nie moga˛ na siebie nachodzić. Łańcuch docelowy t musi być wystarczajaco ˛ długi, aby zmieścić wynik. Funkcja strcat zwraca wskaźnik do łańcucha docelowego t. char* strncat(char* t, char const* s, size_t n); ◮ Dołancza co najwyżej n znaków z łańcucha wskazywanego przez s do łańcucha wskazywanego przez t. Programowanie Proceduralne Biblioteka string.h strcat() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char* s1 = "Linux is fun, "; char* s2 = "Slackware more fun, "; char* s3 = "so have fun!"; char* t = calloc(strlen(s1) + strlen(s2) + strlen(s3) + 1, sizeof(char)); strcpy(t, ""); strcat(t, s1); strcat(t, s2); strcat(t, s3); printf("%d : %s\n", strlen(t), t); free(t); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik 46 : Linux is fun, Slackware more fun, so have fun! Programowanie Proceduralne Biblioteka string.h strcat() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char* s1 = "Linux is fun, "; char* s2 = "Slackware more fun, "; char* s3 = "so have fun!"; char* t = calloc(strlen(s1) + strlen(s2) + strlen(s3) + 1, sizeof(char)); strcat(strcat(strcpy(t, s1), s2), s3); // mniej czytelne !!! printf("%d : %s\n", strlen(t), t); free(t); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik 46 : Linux is fun, Slackware more fun, so have fun! Programowanie Proceduralne Biblioteka string.h strchr ◮ char *strchr(const char *s, int c); ◮ ◮ Funkcja zwraca wskaźnik do pierwszego wysta̧pienia znaku c w łańcuchu s, lub NULL jeśli znaku c nie ma w łańcuchu s. char *strrchr(const char *s, int c); ◮ Funkcja zwraca wskaźnik do ostatniego wysta̧pienia znaku c w łańcuchu s, lub NULL jeśli znaku c nie ma w łańcuchu s. Programowanie Proceduralne Biblioteka string.h strrchr() - Przykładowy program 1 #include <stdio.h> #include <string.h> int main (void) { char str[] = "This is a sample string"; char *pch, ch; puts("Podaj znak: "); scanf("%c",&ch); pch=strrchr(str,ch); if (pch) printf ("Ostatnie wystapienie ’%c’ w \"%s\" zostalo znalezione na pozycji %d \n",ch,str,pch-str+1); else printf ("Znaku ’%c’ nie ma w \"%s\" \n",ch,str); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik ◮ Podaj znak: a Ostatnie wystapienie ’a’ w "This is a sample string" zostalo znalezione na pozycji 12 ◮ Podaj znak: i Ostatnie wystapienie ’i’ w "This is a sample string" zostalo znalezione na pozycji 21 ◮ Podaj znak: b Znaku ’b’ nie ma w "This is a sample string" Programowanie Proceduralne Biblioteka string.h fgets ◮ char *fgets(char *s, int size, FILE *stream); ◮ ◮ Funkcja czyta co najmniej size znaków ze strumienia stream i umieszcza je w buforze wskazywanym prze s. Funkcja kończy czytanie po wczytaniu znaku nowej lini lub znaku EOF. Jeśli nowa linia zostaje przeczytana, to pozostaje on w buforze. Znak ’\0’ jest dopisywany za ostatnim przeczytanmy znakiem. Programowanie Proceduralne Biblioteka string.h fgets() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *s1 = "Hello ", *s2 = " ! How are you ?\n"; int const maxLen = 64; char name[maxLen]; printf("Podaj swoje imie i nazwisko : \n"); fgets(name, maxLen, stdin); char* p = strchr(name, ’\n’); if (p != NULL) { *p = ’\0’; } int const n = strlen(s1) + strlen(name) + strlen(s2) + 1; char* t = calloc(n, sizeof(char)); strcat(strcat(strcpy(t, s1), name), s2); printf("\n%s\n", t); free(t); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik Podaj swoje imie i nazwisko : Jan Kowalski i Bozena Wozna Hello Jan Kowalski i Bozena Wozna ! How are you ? Programowanie Proceduralne Biblioteka string.h ◮ char* strchr(char const* s, int c); ◮ ◮ char* strrchr(char const* s, int c); ◮ ◮ Zwraca wskaźnik do pierwszego wystapienia ˛ znaku c w łańcuchu s Zwraca wskaźnik do ostatniego wystapienia ˛ znaku c w łańcuchu s char* strpbrk(char const* s, char const* accept; ◮ Zwraca wskaźnik do pierwszego wystapienia ˛ dowolnego znaku z łańcucha accept w łańcuchu s. Programowanie Proceduralne Biblioteka string.h strpbrk() - Przykładowy program 1 #include<string.h> #include<stdio.h> int main(void) { char string[]="Welcome in AJD !"; char *string_ptr; printf("Old string is \"%s\".\n",string); while((string_ptr=strpbrk(string," "))!=NULL) *string_ptr=’-’; printf("New string is \"%s\".\n",string); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik Old string is "Welcome in AJD !". New string is "Welcome-in-AJD-!". Programowanie Proceduralne Biblioteka string.h strpbrk() - Przykładowy program 2 #include <stdio.h> #include <string.h> int main (void) { char str[] = "This is a sample string"; char key[] = "aeiou"; char * pch; printf ("Vowels in ’%s’: ",str); pch = strpbrk (str, key); while (pch != NULL){ printf ("%c " , *pch); pch = strpbrk (pch+1,key); } printf ("\n"); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik Vowels in ’This is a sample string’: i i a a e i Programowanie Proceduralne Biblioteka string.h strcmp, strncmp ◮ int strcmp(char const * s1, char const* s2); ◮ ◮ ◮ ◮ ◮ Porównuje łańcuch s1 z łańcuchem s2 Zwraca liczbe˛ < 0, jeżeli s1 < s2 Zwraca 0, jeżeli s1 == s2 Zwraca liczbe˛ > 0, jeżeli s1 > s2 int strncmp(char const * s1, char const* s2, size_t n); ◮ ◮ Porównuje co najwyżej n poczakowych ˛ znaków z łańcuchów s1 i s2. Zwraca takie same wartości jak funkcja strcmp. Programowanie Proceduralne Biblioteka string.h strcmp() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int const Rozmiar=512; char s[Rozmiar], t[Rozmiar]; puts("Podaj pierwsze zdanie"); fgets(s,Rozmiar,stdin); char *p=strchr(s,’\n’); *p=’\0’; puts("Podaj drugie zdanie"); fgets(t,Rozmiar,stdin); p=strchr(t,’\n’); *p=’\0’; int n=strcmp(s,t); if (n==0){ printf("%s = %s\n", s, t); } if (n>0){ printf("%s > %s\n", s, t); } if (n<0){ printf("%s < %s\n", s, t); } return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik ◮ ◮ ◮ Podaj pierwsze zdanie Jan Kowalski Podaj drugie zdanie Jan Kowalski Jan Kowalski = Jan Kowalski Podaj pierwsze zdanie Czestochowa Podaj drugie zdanie Warszawa Czestochowa < Warszawa Podaj pierwsze zdanie Polska Podaj drugie zdanie Anglia Polska > Anglia Programowanie Proceduralne Biblioteka string.h strcmp() - Przykładowy program #include <stdio.h> #include <string.h> int main(void) { char szKey[] = "apple"; char szInput[80]; do { printf ("Which is my favourite fruit? "); gets (szInput); } while (strcmp (szKey,szInput) != 0); printf ("Correct answer!\n"); return 0; } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik Which is my favourite fruit? orange Which is my favourite fruit? apple Correct answer! Programowanie Proceduralne Biblioteka string.h strstr ◮ char* strstr(char const * haystack, char const* needle); ◮ ◮ ◮ ◮ Znajduje pierwsze wystapienie ˛ podłańcucha needle w łańcuchu haystack. Kończace ˛ znaki NULL nie sa˛ porównywane. Zwraca wskaźnik do poczatku ˛ podłańcucha, jeżeli podłańcuch został‚ znaleziony. Zwraca wskaźnik NULL, jeżli podłańcuch nie został‚ znaleziony. Programowanie Proceduralne Biblioteka string.h strstr() - Przykładowy program char* strrstr(char const* haystack, char const* needle) { char *last, *current; // Inicjalizacja wskaznika dla ostatniego znalezionego wystapienia. ˛ last = NULL; // Wyszukiwanie tylko wtedy, gdy drugi ciag nie jest pusty. // Jezeli ciag needle jest pusty, funkcja zwraca NULL. if (*needle != ’\0’) { current = strstr(haystack, needle); // Za kazdym znalezieniem ciagu zapamietaj wskaznik //do jego poczatku // Nastepnie szukaj nastepnego wystapienia ciagu. while (current != NULL) { last = current; current = strstr(last + 1, needle); } } return last; } Programowanie Proceduralne Biblioteka string.h strstr() - Przykładowy program #include <stdio.h> #include <stdlib.h> #include <string.h> char* strrstr(char const* haystack, char const* needle); int main(void){ char *s = "Linux is cool ! Linux is fun ! Linux is the best !"; char *t = "Linux"; char *w = strstr(s,t); puts (s); puts (t); puts (w); w = strrstr(s,t); puts (w); return 0; } char* strrstr(char const* haystack, char const* needle) { // definicja z poprzedniego slajdu } Programowanie Proceduralne Biblioteka string.h Przykładowy program - wynik Linux is cool ! Linux is fun ! Linux is the best ! Linux Linux is cool ! Linux is fun ! Linux is the best ! Linux is the best !