综习 11-1
将代码清单11-3中对p的赋值进行如下修改。
p= “456” +1;
请编写程序确认运行结果,并对运行结果进行分析。
#include <stdio.h>
int main(void) {
char *p = "ABC" + 1;
puts(p);
return 0;
}
练习11-2
在代码清单11-4中,各数组的字符串个数3是作为常量嵌在程序(for语句的控制表达式)中的。请编写一段程序,将其改写为通过计算求出。
#include <stdio.h>
int main(void) {
char a[][5] = {"LISA", "ASS", "BBC"};
char *b[] = {"abc", "cca", "wc"};
int i;
for (i = 0; i < sizeof a / sizeof a[0]; ++i)
printf("a[%d] = \"%s\"\n", i, a[i]);
for (i = 0; i < sizeof b / sizeof b[0]; ++i)
printf("b[%d] = \"%s\"\n", i, b[i]);
return 0;
}
练习11-3
改写代码清单11-6的程序,将本文中学习的str_copy函数的调用作为printf函数的实参。
#include <stdio.h>
char *str_copy(char *s1, const char *s2) {
char *p = s1;
while (*s1 = *s2) {
++s1;
++s2;
}
return p;
}
int main(void) {
char a[10] = "你好!";
char b[10];
puts(str_copy(b, a));
return 0;
}
练习11-4
不使用下标运算符,编写如下函数,显示字符串s。
void put string (const char s){/…*/}
#include <stdio.h>
void put_string(const char s[]) {
while (*s)
putchar(*s++);
}
int main(void) {
char a[10] = "你好!";
put_string(a);
putchar('\n');
return 0;
}
练习11-5
不使用下标运算符,编写如下函数,返回字符串s中字符c的个数(若不存在,则为0).
int str chnum(const char s, int c){/…*/}
#include <stdio.h>
int str_chnum(const char s[], int c) {
int num = 0;
while (*s)
if (*s++ == c)
num += 1;
return num;
}
int main(void) {
printf("%d\n", str_chnum("hello", 'l'));
return 0;
}
练习11-6
不使用下标运算符,编写如下函数,若字符串s中含有字符c(若含有多个,以先出现的为准),则返回指向该字符的指针,否则返回空指针。
char str chr(const char s,int c){/…/ }
#include <stdio.h>
char *str_chr(const char s[], int c) {
while (*s) {
if (*s == c)
return s;
++s;
}
return NULL;
}
int main(void) {
puts(str_chr("hello", 'l'));
return 0;
}
练习11-7
不使用下标运算符,实现代码清单9-11的str_toupper函数和str_toLower函数。
#include <stdio.h>
#include <ctype.h>
void str_toupper(char *s) {
while(*s) {
*s = toupper(*s);
++s;
}
}
void str_tolower(char *s) {
while(*s) {
*s = tolower(*s);
++s;
}
}
int main(void) {
char str[] = "Hello World";
puts(str);
str_toupper(str);
puts(str);
str_tolower(str);
puts(str);
return 0;
}
练习11-8
编写如下函数,删除字符串str内的所有数字字符。
void del digit(char str) {/…*/}
例如,如果接收"AB1C9",就返回“ABC"。注意不要使用下标运算符。
#include <stdio.h>
#include <ctype.h>
void del_digit(char s[]) {
char *p = s;
while (*s) {
if (!isdigit(*s))
*p++ = *s;
++s;
}
*p = '\0';
}
int main(void) {
char s[] = "AB1C9";
puts(s);
del_digit(s);
puts(s);
return 0;
}
练习11-9
使用本节中学习的库函数(strLen函数、strcpy函数、strncpy函数、strcat函数、strncat函数、strcmp函数、strncmp 函数)编写程序。
#include <stdio.h>
#include <string.h>
int main(void) {
char str[12] = "Hello World";
char t[12];
printf("%zu\n", strlen(str));
strncpy(t, str, 5);
t[5] = '\0';
puts(t);
printf("%d\n", strcmp(str, t));
printf("%d\n", strncmp(str, t, 5));
strcat(t, str + 5);
puts(t);
strlwr(t);
puts(t);
return 0;
}
练习11-10
编写如下函数,实现与库函数atoi、atol、atof相同的功能。
intstrtoi (const char * nptr){/…/}
long strtol (const char * nptr)(/…/}
double strtof (const char nptr){/ …*/)
#include <stdio.h>
#include <ctype.h>
#define to_digit(n) (n - '0')
int str_toi(const char s[]) {
int result = 0;
int sign = 1;
switch (*s) {
case '-': sign = -1;
case '+': ++s;
}
while (isdigit(*s))
result = result * 10 + to_digit(*s++);
return result * sign;
}
long str_tol(const char s[]) {
long result = 0;
int sign = 1;
switch (*s) {
case '-': sign = -1;
case '+': ++s;
}
while (isdigit(*s))
result = result * 10 + to_digit(*s++);
return result * sign;
}
double str_tof(const char s[]) {
double result = 0;
int sign = 1;
int t;
switch (*s) {
case '-': sign = -1;
case '+': ++s;
}
while (isdigit(*s))
result = result * 10 + to_digit(*s++);
if (*s == '.') {
t = 1;
while (isdigit(*++s)) {
result = result * 10 + to_digit(*s);
t *= 10;
}
result /= t;
}
result *= sign;
if (*s == 'e' || *s == 'E') {
sign = 1;
t = 0;
switch (*++s) {
case '-': sign = -1;
case '+': ++s;
}
while (isdigit(*s))
t = t * 10 + to_digit(*s++);
// 按符号操作
if (sign == 1)
while (--t != -1)
result *= 10;
else
while (--t != -1)
result /= 10;
}
return result;
}
int main(void) {
printf("%f\n", str_tof("-13.5E+2"));
printf("%f\n", -13.5E+2);
return 0;
}