Unit 6: Strings - Tin học đại cương (IT1110) | Trường Đại học Bách khoa Hà Nội

Unit 6: Strings

lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
Character Strings
lOMoARcPSD| 27879799
Character Strings
lOMoARcPSD| 27879799
Character Strings
lOMoARcPSD| 27879799
int getchar (void) ;
Don’t confuse with getch() of conio.h
s
lOMoARcPSD| 27879799
s
followed by \n */
lOMoARcPSD| 27879799
Character classicaon funcons
lOMoARcPSD| 27879799
String-processing funcons
lOMoARcPSD| 27879799
String-processing funcons
size_t strlen(char[] s)
Get length of string s
prin("%d ",strlen("Hello world")); 11
char[] strcpy(char[] str1, char[] str2)
Copy string string str2 to string str1
prin("%s ",strcpy(Str,"Hello")); Hello
prin("%s", Str); Hello
int strcmp(char[] str1, char[] str2)
Compare str1 to str2
lOMoARcPSD| 27879799
Return 0 if they are equal to each other;
Return a value < 0: str1 < str2
Return a value >0: str1 > str2
String-processing funcons
char[] strcat(char[] str1, char[] str2)
Appends a copy of the string str2 to the end of
the string str1. ...
The strcat funcon returns a pointer to str1
Example
char Str[20];
lOMoARcPSD| 27879799
strcpy(Str,"Hello "); prin("%s
",strcat(Str,"world")); Helloworld
prin("\n%s",Str); Helloworld
String-processing funcons
char * strchr (char * s, int c)
Searches for the rst occurrence of the
character c (an unsigned char) in the string
pointed to by the argument s
. strcpy(Str,"Hello world"); prin("%s
",strchr(Str,o')); o world
char* strstr(char * str1, char * str2)
lOMoARcPSD| 27879799
Return a pointer to the rst occurrence in str2
of the enre sequence of characters specied in
str1, or a null pointer if the sequence is not
present in str1.
prin("%s ",strstr(Str,”llo”)); llo world
Searching using funcon strstr
1.#include<stdio.h>
2.#include<conio.h>
3.#include<string.h>
4.main()
5.{char s[20],u[20];
6.int m,i, n;
lOMoARcPSD| 27879799
7.puts("Enter your string");gets(s);
8.ush(stdin);
9.puts("Enter string you want to search");gets(u);
10.if(strstr(s,u)==NULL)puts("False"); else puts("True");
11.}
lOMoARcPSD| 27879799
s, the program prints zero
lOMoARcPSD| 27879799
| 1/15

Preview text:

lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 Character Strings lOMoAR cPSD| 27879799 Character Strings lOMoAR cPSD| 27879799 Character Strings lOMoAR cPSD| 27879799 int getchar (void) ;
Don’t confuse with getch() of conio.h s lOMoAR cPSD| 27879799 s followed by \n */ lOMoAR cPSD| 27879799
Character classification functions lOMoAR cPSD| 27879799 String-processing functions lOMoAR cPSD| 27879799 String-processing functions • size_t strlen(char[] s) • Get length of string s
• printf("%d ",strlen("Hello world")); 11
• char[] strcpy(char[] str1, char[] str2)
• Copy string string str2 to string str1
• printf("%s ",strcpy(Str,"Hello")); Hello • printf("%s", Str); Hello
• int strcmp(char[] str1, char[] str2) • Compare str1 to str2 lOMoAR cPSD| 27879799
• Return 0 if they are equal to each other;
• Return a value < 0: str1 < str2
• Return a value >0: str1 > str2 String-processing functions
• char[] strcat(char[] str1, char[] str2)
• Appends a copy of the string str2 to the end of the string str1. ...
• The strcat function returns a pointer to str1 • Example char Str[20]; lOMoAR cPSD| 27879799
strcpy(Str,"Hello "); printf("%s
",strcat(Str,"world")); Helloworld
printf("\n%s",Str); Helloworld String-processing functions
• char * strchr (char * s, int c)
• Searches for the first occurrence of the
character c (an unsigned char) in the string pointed to by the argument s
• . strcpy(Str,"Hello world"); printf("%s ",strchr(Str,‘o')); o world
• char* strstr(char * str1, char * str2) lOMoAR cPSD| 27879799
• Return a pointer to the first occurrence in str2
of the entire sequence of characters specified in
str1, or a null pointer if the sequence is not present in str1.
printf("%s ",strstr(Str,”llo”)); llo world
Searching using function strstr 1.#include 2.#include 3.#include 4.main() 5.{char s[20],u[20]; 6.int m,i, n; lOMoAR cPSD| 27879799
7.puts("Enter your string");gets(s); 8.fflush(stdin);
9.puts("Enter string you want to search");gets(u);
10.if(strstr(s,u)==NULL)puts("False"); else puts("True"); 11.} lOMoAR cPSD| 27879799 s, the program prints zero lOMoAR cPSD| 27879799