PROGRAMMING:Code analysis of string function
Please analyze the results of the following code, and discuss the specific string function usage involved in the program.
```
Code 1:
#include
#include
int main(){
char s[80]={"Harbin Normal University"};
char t[80]={"Harbin"};
int i;
Printf ("the string \% s \" has% d characters. N ", t, strlen (T));
for(i=0; iPrintf ("subscript% d: C, n", I, t [i]);
strlwr(s);
puts(s);
puts(strupr(s));
}
Execute program, output:
The string "Harbin" has six characters
Subscript 0: H
Subscript 1: a
Subscript 2: R
Subscript 3: B
Subscript 4: I
Subscript 5: n
harbin normal university
HARBIN NORMAL UNIVERSITY
Code 2:
#include
#include
int main(){
char s[80]={"Harbin "};
char t[80]={"University"};
strcat(s,"Normal "); strcat(s,t); puts(s);
strcpy(s,"HeiLongJiang"); puts(s);
strrev(s); puts(s);
strset(s,'A'); puts(s);
}
Execute program, output:
Harbin Normal University
HeiLongJiang
gnaiJgnoLieH
AAAAAAAAAAAA
Code 3:
#include
#include
int main(){
char s[80]={"Harbin Normal University"};
char c;
Printf ("please enter the character you want to find:);
c=getchar();
Printf ("character% C in string \% s \", C, s));
if(strchr(s,c)!= NULL)
Printf ("subscript index of is% d.), strchr (s, c) - s)";
else
Printf ("not found!");
}
Execute the program, input y, output:
Please enter the character you want to find: y
The subscript index of the character y in the string "Harbin Normal University" is: 23
Execute the program again, input K, output:
Please enter the character you want to find: K
The character K is not found in the string "Harbin Normal University"!
Program analysis:
The return value of the function strchr (s, c) is the memory address value of the character found (null is returned if not found). S is the first address of the array (that is, the address of s [0]). The result of subtracting the addresses of two elements in the array can be understood as the difference between the subscripts of two elements (integer type).
Code 4:
#include
#include
int main(){
char s[80]={"Harbin Normal University"};
char t[80]={"sity"};
Printf ("please enter the string you want to find):";
gets(t);
Printf ("string \% s \" in string \% s \ ", t, s));
if(strstr(s,t)!= NULL)
Printf ("subscript index of is: D., strstr (s, t) - S.);
else
Printf ("not found!");
}
Execute the program, input sex, output:
Please enter the string you want to find: size
The subscript index of the string "sex" in the string "Harbin Normal University" is: 20
Execute the program again, input Heilongjiang, output:
Please enter the string you want to find: Heilongjiang
String "Heilongjiang" not found in string "Harbin Normal University"!
```
Combined with the analysis, discussion and study of the above code, please complete the following topics:
Input a sentence and a word, output whether the word is included in the sentence.
###Input format:
One line is a sentence and the other is a word.
###Output format:
Output yes! Or not found!
###Input example:
```in
Harbin Normal University
teach
```
###Output example:
```out
Not Found!
```
###Input example:
```in
Harbin Normal University
Nor
```
###Output example:
```out
YES!
```
answer:If there is no answer, please comment
```
Code 1:
#include
#include
int main(){
char s[80]={"Harbin Normal University"};
char t[80]={"Harbin"};
int i;
Printf ("the string \% s \" has% d characters. N ", t, strlen (T));
for(i=0; i
strlwr(s);
puts(s);
puts(strupr(s));
}
Execute program, output:
The string "Harbin" has six characters
Subscript 0: H
Subscript 1: a
Subscript 2: R
Subscript 3: B
Subscript 4: I
Subscript 5: n
harbin normal university
HARBIN NORMAL UNIVERSITY
Code 2:
#include
#include
int main(){
char s[80]={"Harbin "};
char t[80]={"University"};
strcat(s,"Normal "); strcat(s,t); puts(s);
strcpy(s,"HeiLongJiang"); puts(s);
strrev(s); puts(s);
strset(s,'A'); puts(s);
}
Execute program, output:
Harbin Normal University
HeiLongJiang
gnaiJgnoLieH
AAAAAAAAAAAA
Code 3:
#include
#include
int main(){
char s[80]={"Harbin Normal University"};
char c;
Printf ("please enter the character you want to find:);
c=getchar();
Printf ("character% C in string \% s \", C, s));
if(strchr(s,c)!= NULL)
Printf ("subscript index of is% d.), strchr (s, c) - s)";
else
Printf ("not found!");
}
Execute the program, input y, output:
Please enter the character you want to find: y
The subscript index of the character y in the string "Harbin Normal University" is: 23
Execute the program again, input K, output:
Please enter the character you want to find: K
The character K is not found in the string "Harbin Normal University"!
Program analysis:
The return value of the function strchr (s, c) is the memory address value of the character found (null is returned if not found). S is the first address of the array (that is, the address of s [0]). The result of subtracting the addresses of two elements in the array can be understood as the difference between the subscripts of two elements (integer type).
Code 4:
#include
#include
int main(){
char s[80]={"Harbin Normal University"};
char t[80]={"sity"};
Printf ("please enter the string you want to find):";
gets(t);
Printf ("string \% s \" in string \% s \ ", t, s));
if(strstr(s,t)!= NULL)
Printf ("subscript index of is: D., strstr (s, t) - S.);
else
Printf ("not found!");
}
Execute the program, input sex, output:
Please enter the string you want to find: size
The subscript index of the string "sex" in the string "Harbin Normal University" is: 20
Execute the program again, input Heilongjiang, output:
Please enter the string you want to find: Heilongjiang
String "Heilongjiang" not found in string "Harbin Normal University"!
```
Combined with the analysis, discussion and study of the above code, please complete the following topics:
Input a sentence and a word, output whether the word is included in the sentence.
###Input format:
One line is a sentence and the other is a word.
###Output format:
Output yes! Or not found!
###Input example:
```in
Harbin Normal University
teach
```
###Output example:
```out
Not Found!
```
###Input example:
```in
Harbin Normal University
Nor
```
###Output example:
```out
YES!
```
answer:If there is no answer, please comment