Programowanie Proceduralne Bożena Woźna-Szcześniak bwozna@gmail.com Jan Długosz University, Poland Wykład 7
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; }
Przykładowy program - wynik 14 : Linux is fun! 14 : Linux is fun!
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ć kopię s. Funkcja strcpy zwraca wskaźnik do łańcucha docelowego t.
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; }
Przykładowy program - wynik 14 : Linux is fun! 0 : 14 : Linux is fun!
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 będzie znaku NULL, łańcuch t nie będzie 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
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; }
Przykładowy program - wynik 31 : This program is free software! 9 : This prog 9 : This prog
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; }
Przykładowy program - wynik 31 : This program is free software! 9 : This prog 10 : This progr
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ć wystarczająco 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.
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; }
Przykładowy program - wynik 46 : Linux is fun, Slackware more fun, so have fun!
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; }
Przykładowy program - wynik 46 : Linux is fun, Slackware more fun, so have fun!
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.
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; }
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"
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.
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; }
Przykładowy program - wynik Podaj swoje imie i nazwisko : Jan Kowalski i Bozena Wozna Hello Jan Kowalski i Bozena Wozna! How are you?
char* strchr(char const* s, int c); Zwraca wskaźnik do pierwszego wystapienia znaku c w łańcuchu s char* strrchr(char const* s, int c); 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.
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; }
Przykładowy program - wynik Old string is "Welcome in AJD!". New string is "Welcome-in-AJD-!".
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; }
Przykładowy program - wynik Vowels in This is a sample string : i i a a e i
strcmp, strncmp int strcmp(char const * s1, char const* s2); Porównuje łańcuch s1 z łańcuchem s2 Zwraca liczbę < 0, jeżeli s1 < s2 Zwraca 0, jeżeli s1 == s2 Zwraca liczbę > 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.
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; }
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
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; }
Przykładowy program - wynik Which is my favourite fruit? orange Which is my favourite fruit? apple Correct answer!
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.
strstr() - Przykładowy program char* strrstr(char const* haystack, char const* needle) { char *last, *current; // Inicjalizacja wskaznika dla ostatniego znalezionego wystąpienia. 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; }
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 }
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!