📄 chap12.lst
字号:
listing 1
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isalnum(ch)) printf("%c is alphanumeric\n", ch);
}
return 0;
}
listing 2
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isalpha(ch)) printf("%c is a letter\n", ch);
}
return 0;
}
listing 3
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isascii(ch)) printf("%c is ASCII defined\n", ch);
}
return 0;
}
listing 4
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(iscntrl(ch)) printf("%c is a control character\n", ch);
}
return 0;
}
listing 5
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isdigit(ch)) printf("%c is a digit\n", ch);
}
return 0;
}
listing 6
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isgraph(ch)) printf("%c is a printing character\n", ch);
}
return 0;
}
listing 7
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(islower(ch)) printf("%c is lowercase\n", ch);
}
return 0;
}
listing 8
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isprint(ch)) printf("%c is printable\n", ch);
}
return 0;
}
listing 9
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(ispunct(ch)) printf("%c is punctuation\n", ch);
}
return 0;
}
listing 10
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch=='.') break;
if(isspace(ch)) printf("%c is white-space\n", ch);
}
return 0;
}
listing 11
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isupper(ch)) printf("%c is upper-case\n", ch);
}
return 0;
}
listing 12
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
for(;;) {
ch = getchar();
if(ch==' ') break;
if(isxdigit(ch)) printf("%c is hexadecimal \n", ch);
}
return 0;
}
listing 13
char str[20], out[20];
strcpy(str, "hello there");
memccpy(out, str,' ', 20);
listing 14
#include <stdio.h>
#include <string.h>
int main(void)
{
void *p;
p = memchr("this is a test", ' ', 14);
printf((char *) p);
return 0;
}
listing 15
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int outcome;
size_t len, l1, l2;
if(argc != 3) {
printf("Use two command-line args.");
return 1;
}
/* find the length of shortest */
len = (l1=strlen(argv[1]))<(l2=strlen(argv[2])) ? l1:l2;
outcome = memcmp(argv[1], argv[2], len);
if(!outcome) printf("equal");
else if(outcome<0) printf("First less than second.\n");
else printf("First greater than second\n");
return 0;
}
listing 16
#include <stdio.h>
#include <string.h>
#define SIZE 80
int main(void)
{
char buf1[SIZE], buf2[SIZE];
strcpy(buf1, "When, in the course of...");
memcpy(buf2, buf1, SIZE);
printf(buf2);
return 0;
}
listing 17
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[40], str2[40];
strcpy(str1, "Born to code in C/C++.");
memmove(str2, str1, strlen(str1)+1);
printf(str2);
return 0;
}
listing 18
memset(buf, '\0', 100);
memset(buf, 'X', 10);
printf((char *) buf);
listing 19
char str[8];
stpcpy(str, "hello");
listing 20
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[80], s2[80];
gets(s1);
gets(s2);
strcat(s2, s1);
printf(s2);
return 0;
}
listing 21
#include <stdio.h>
#include <string.h>
int main(void)
{
char *p;
p = strchr("this is a test", ' ');
printf(p);
return 0;
}
listing 22
int password(void)
{
char s[80];
printf("Enter password: ");
gets(s);
if(strcmp(s, "pass")) {
printf("Invalid password.\n");
return 0;
}
return 1;
}
listing 23
char str[80];
strcpy(str, "hello");
listing 24
#include <stdio.h>
#include <string.h>
int main(void)
{
int len;
len = strcspn("this is a test", "ab");
printf("%d", len);
return 0;
}
listing 25
char str[80], *p;
strcpy(str, "this is a test");
p = strdup(str);
listing 26
void swap()
{
/* ... */
if(error) printf(_strerror("Error in swap."));
listing 27
if(errno) printf(strerror(errno));
listing 28
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if(argc != 3) {
printf("Use two command-line args.");
return 1;
}
if(!stricmp(argv[1], argv[2]))
printf("The filenames are the same.\n");
else
printf("The filenames differ.\n");
return 0;
}
listing 29
strcpy(s, "hello");
printf("%d", strlen(s));
listing 30
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[80];
strcpy(s, "THIS IS A TEST");
strlwr(s);
printf(s);
return 0;
}
listing 31
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[80], s2[80];
size_t len;
gets(s1);
gets(s2);
/* compute how many chars will actually fit */
len = 79-strlen(s2);
strncat(s2, s1, len);
printf(s2);
return 0;
}
listing 32
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if(argc != 3) {
printf("Use two command-line args.");
return 1;
}
if(!strnicmp(argv[1], argv[2], 8))
printf("The filenames are the same.\n");
else
printf("The filenames differ.\n");
return 0;
}
listing 33
char str1[128], str2[80];
gets(str1);
strncpy(str2, str1, 79);
listing 34
strnset(str, 'x', 10);
listing 35
#include <stdio.h>
#include <string.h>
int main(void)
{
char *p;
p = strpbrk("this is a test", " absj");
printf(p);
return 0;
}
listing 36
#include <stdio.h>
#include <string.h>
int main(void)
{
char *p;
p = strrchr("this is a test", 'i');
printf(p);
return 0;
}
listing 37
#include <stdio.h>
#include <string.h>
char s[] = "hello";
int main(void)
{
strrev(s);
printf(s);
return 0;
}
listing 38
strset(str, 'x');
listing 39
#include <stdio.h>
#include <string.h>
int main(void)
{
int len;
len = strspn("this is a test", "siht ");
printf("%d",len);
return 0;
}
listing 40
#include <stdio.h>
#include <string.h>
int main(void)
{
char *p;
p = strstr("this is a test", "is");
printf(p);
return 0;
}
listing 41
#include <stdio.h>
#include <string.h>
int main(void)
{
char *p;
p = strtok("The summer soldier, the sunshine patriot"," ");
printf(p);
do {
p=strtok('\0', ", ");
if(p) printf("|%s", p);
} while(p);
return 0;
}
listing 42
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[80];
strcpy(s, "this is a test");
strupr(s);
printf(s);
return 0;
}
listing 43
putchar(tolower('Q'));
listing 44
putchar(toupper('a'));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -