9.1
#include <stdio.h>
#include <ctype.h>
#include <string.h> #define N 100 int main (void)
{
int i = ;
char strarray[N+]; //= "Hello world! \t3I'm here!";
char *input = strarray;
char ch;
unsigned int cont = ;
int ctr=, spc=, num=, low=, upp=, puc=, upr=;
float ctrf=, spcf=, numf=, lowf=, uppf=, pucf=, uprf=; printf("Please enter a string:\n"); while (( ch = getchar() ) != '\n')
{
*(input + i) = ch;
i++;
}
*(input + i) = '\0';
cont = strlen(input); while (*input != '\0')
{
if (iscntrl(*input))
ctr++;
if (isspace(*input))
spc++;
if (isdigit(*input))
num++;
if (islower(*input))
low++;
if (isupper(*input))
upp++;
if (ispunct(*input))
puc++;
if (!(isprint(*input)))
upr++;
input++;
} printf("\nTotle char number is: Count = %d\n", cont);
printf("cntrl = %d,\tspace = %d,\tdigit = %d\n", ctr, spc, num);
printf("lower = %d,\tupper = %d,\tpunct = %d\n", low, upp, puc);
printf("uprint = %d\n", upr); ctrf = (float)ctr/(float)cont;
spcf = (float)spc/(float)cont;
numf = (float)num/(float)cont;
lowf = (float)low/(float)cont;
uppf = (float)upp/(float)cont;
pucf = (float)puc/(float)cont;
uprf = (float)upr/(float)cont; printf("\nHere is the percent of every count:\n");
printf("cntrl = %.3f%%\nspace = %.3f%%\ndigit = %.3f%%\n",ctrf* , spcf*, numf*);
printf("lower = %.3f%%\nupper = %.3f%%\npunct = %.3f%%\n", lowf*, uppf*, pucf*);
printf("uprnt = %.3f%%\n", uprf*); getchar();
return ;
}
9.3
#include <stdio.h>
#include <string.h> void my_strcpy(char *dst, char *src)
{
size_t len = , dstlen = , srclen = ;
dstlen = strlen(dst);
srclen = strlen(src); len = ( dstlen >= srclen ? srclen : dstlen );
strncpy(dst, src, len);
} void show_string(char *str)
{
while (*str != '\0')
printf("%c", *str++);
putchar('\n');
} int main (void)
{
char dst[] = "Hello";
char src[] = "I'm here!";
char *dstp = dst;
char *srcp = src; my_strcpy(dstp, srcp); show_string(dstp);
show_string(srcp); getchar();
return ;
}
9.4
#include <stdio.h>
#include <string.h> #define M 12
#define N 16 void my_strcat(char *dst, char *src)
{
size_t len = , dstlen = , srclen = , restlen = ;
dstlen = strlen(dst);
srclen = strlen(src); len = ( (restlen = M - dstlen - ) > srclen ? srclen : restlen );
strncat(dst, src, len);
} void show_string(char *str)
{
while (*str != '\0')
printf("%c", *str++);
putchar('\n');
} int main (void)
{
char dst[M] = "Hello!";
char src[N] = "I'm here!";
char *dstp = dst;
char *srcp = src; my_strcat(dst, src); show_string(dstp);
show_string(srcp); getchar();
return ;
}
9.5
#include <stdio.h>
#include <string.h> #define M 12
#define N 16 void my_strncat(char *dest, char *src, int dest_len)
{
size_t len = , dstlen = , srclen = , restlen = ;
dstlen = strlen(dest);
srclen = strlen(src); len = ( (restlen = dest_len - dstlen - ) > srclen ? srclen : restlen );
strncat(dest, src, len);
} void show_string(char *str)
{
while (*str != '\0')
printf("%c", *str++);
putchar('\n');
} int main (void)
{
char dst[M] = "Hello!";
char src[N] = "I'm here!";
char *dstp = dst;
char *srcp = src; my_strncat(dst, src, M); show_string(dstp);
show_string(srcp); getchar();
return ;
}
9.6
#include <stdio.h>
#include <string.h> char *my_strcpy_end(char *dst, char *src)
{
int i, dstlen = , srclen = ; dstlen = strlen(dst);
srclen = strlen(src); if ( dstlen >= srclen )
strcpy(dst, src);
else
strncpy(dst, src, dstlen); for ( i = ; *(dst + i) != '\0'; i++);
return dst + i;
} int main (void)
{
char *p = NULL;
char dst[] = "Hello world";
//char src[] = "123456789";
char src[] = ""; p = my_strcpy_end(dst, src); printf("%c\n", *(p - )); getchar();
return ;
}
9.8
#include <stdio.h>
#include <string.h> char *my_strnchr( char const *str, int ch, int which )
{
int i;
char *p = NULL;
p = str; for ( i = ; i < which; i++)
{
if ( i != )
p++;
p = strchr(p, ch);
} return p;
} int main (void)
{
int i;
char str[] = "Hello world!";
int chr = 'l';
int which = ; char *find = my_strnchr(str, chr, which); for (i = -; i < ; i++)
printf("%c\t", *(find + i));
putchar('\n'); getchar();
return ;
}
9.9
#include <stdio.h>
#include <string.h> int count_chars(char const *str, char const *chars)
{
int count = , len = ;
char *pos = NULL;
pos = str; while (( pos = strpbrk(pos, chars) ) != NULL )
{
count++;
pos++;
} return count;
} int main (void)
{
char str[] = "Hello world!";
char chars[] = "ero"; printf("%d\n", count_chars(str, chars)); getchar();
return ;
}
9.10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> int palindrome( char *string )
{
int i, j = ;
int len = strlen(string);
char *strcopy = NULL;
strcopy = malloc((len + )* sizeof(char));
for (i = ; i < len; i++) //将源字符串中的非字母字符去掉,并将剩下的字母转换为小写
{
if (isalpha(*(string + i)))
{
*(strcopy + j) = tolower( *(string + i) );
j++;
}
}
*(strcopy + j) = '\0'; len = strlen(strcopy);
for (i = ; i < (len / ); i++)
{
if ( (*(strcopy + i)) != (*(strcopy + (len - i - ) ) ) )
return ;
} return ;
} int main (void)
{
char string[] = "Madam, I'm Adam!";
int result = ; result = palindrome(string);
printf("%d\n", result); getchar();
return ;
}
9.11
#include <stdio.h>
#include <string.h> int main (void)
{
char input[];
char chars[] = "the";
int i = , count = ;
char *pos = NULL; printf("Please enter some words: \n");
scanf("%s", input);
printf("%s\n", input); pos = input;
while ((pos = strstr(pos, chars)) != NULL)
{
pos++;
count++;
} printf("There are %d \"%s\"!", count, chars); getchar();
getchar();
return ;
}
9.12 9.13 9.14
#include <stdio.h>
#include <string.h>
#include <ctype.h> char *init_alp(char *temp)
{
int i;
char alp = 'A'; for (i = ; i < ; i++)
{ *(temp + i) = alp;
alp++;
}
*(temp + i) = '\0'; return temp;
} int prepare_key( char *key ) //生成key
{
int i = , j = , k = ;
char alp[];
char *temp = alp; char *p = NULL; if (key == NULL) //key为空,退出
return ; for (i = ; *(key + i) != '\0'; i++)
if (!(isalpha(*(key + i)))) //key中含有非字母字符,退出)
return ; for (i = ; *(key + i) != '\0'; i++)
{
if (*(key + i) != ' ') //删除所有的空格
{
for (j = i + ; *(key + j) != '\0'; j++) //将重复的字母赋值为空格
{
if ( *(key + i) == *(key + j) )
*(key + j) = ' ';
}
*(key + k) = toupper(*(key + i));
k++;
}
}
*(key + k) = '\0'; temp = init_alp(temp); //初始化为大写字母表
p = temp;
while ((p = (strpbrk(p, key))) != NULL) //将字母表中含有key中的字母设置为空格
{
*p = ' ';
p++;
} for (i = , k = ; *(temp + i) != '\0'; i++) //删除所有的空格
{
if (*(temp + i) != ' ')
{
*(temp + k) = *(temp + i);
k++;
}
}
*(temp + k) = '\0'; key = strcat(key, temp); return ;
} void encrypt( char *data, char const *key ) //给数据加密
{
int i, state;
char temp[];
char *init = NULL;
char *p = NULL;
char chg; init = init_alp(temp); for (i = ; *(data + i) != '\0'; i++)
{
if (isalpha(*(data + i)))
{
if (isupper(*(data + i)))
state = ;
else
{
*(data + i) = toupper(*(data + i));
state = ;
}
p = strchr(init, *(data + i));
chg = *(key + (p - init));
*(data + i) = state ? chg : tolower(chg);
}
}
} void decrypt( char *data, char const *key ) //给数据解密
{
int i, state;
char temp[];
char *init = NULL;
char *p = NULL;
char chg; init = init_alp(temp); for (i = ; *(data + i) != '\0'; i++)
{
if (isalpha(*(data + i)))
{
if (isupper(*(data + i)))
state = ;
else
{
*(data + i) = toupper(*(data + i));
state = ;
}
p = strchr(key, *(data + i));
chg = *(init + (p - key));
*(data + i) = state ? chg : tolower(chg);
}
}
} int main (void)
{
char key[] = "Hellomnnnnab";
char temp[];
char *key_m = temp;
char data[] = "Hello world!"; key_m = init_alp(key_m);
printf("%s\n", key); //显示输入的key if (prepare_key(key))
{
printf("\nAlpha: \n"); //显示大写字母表
printf("%s\n", key_m);
printf("\nKey: \n"); //显示生成的key秘钥
printf("%s\n", key); printf("\nEncode: \n");
printf("%s\n", data);
encrypt(data, key); //加密
printf("%s\n", data); printf("\nDecode: \n");
printf("%s\n", data);
decrypt(data, key); //解密
printf("%s\n", data);
}
else
printf("Key error!\n"); getchar();
return ;
}
9.15
#include <stdio.h>
#include <string.h>
#include <ctype.h> void dollars(char *dest, char const *src)
{
int i, j, srclen = , offset = , state = ;
int a = , b = ;
char *destbak = NULL;
destbak = dest; srclen = strlen(src);
*dest++ = '$'; if (srclen >= )
{
a = (srclen - ) / ;
b = (srclen - ) % ; for (i = ; (i < a); i++)
{
if ( i == )
{
for (j = ; j < b; j++)
{
*(dest++) = *(src++);
}
} if (b != )
*(dest++) = ','; for (j = ; j < ; j++)
*(dest++) = *(src++);
} if ((a == ) && (b != ))
{
for (j = ; j < b; j++)
{
*(dest++) = *(src++);
}
}
*((dest++)) = '.'; for ( ; *(src) != '\0';dest++, src++)
*(dest) = *(src);
*(dest) = '\0';
}
else
{
memset(dest, '', );
*(++dest) = '.';
dest++;
for (i = ; i < - srclen; i++)
*(dest++) = ''; for (i = ; i < srclen; i++)
*(dest + i) = *(src + i);
}
} int dollchk(char *src) //检查要处理的数据中是否有非数字字符
{
int i;
for (i = ; *(src + i) != '\0'; i++)
{
if (!isdigit(*(src + i)))
{
return ;
}
}
return ;
} int main (void)
{
char temp[] = { }; //目标缓存
char dollar[];
char *dest = temp;
int state = ; printf("Please enter some numbers: \n");
scanf("%s", dollar); state = dollchk(dollar);
if (!state) //检查要处理的数据中含有非数字字符,报错,退出
{
printf("Data error!\n");
getchar();
getchar();
return ;
} printf("%s\n", dest);
printf("%s\n", dollar); dollars(dest, dollar);
if (*dest != '\0')
printf("%s\n", dest); getchar();
getchar();
return ;
}
9.16
#include <stdio.h>
#include <string.h> int format( char *format_string, char const *digit_string )
{
int formatlen = strlen(format_string);
int digitlen = strlen(digit_string);
int i, j, k;
char *p = NULL; if (formatlen < digitlen)
return ;
if ((*digit_string) == '\0')
return ; if ((p = strchr(format_string, '.')) != NULL)
{
if ((k = formatlen - (p - format_string)) > digitlen)
{
for (i = formatlen, j = digitlen; j >= ; i--, j--, k--)
{
*(format_string + i) = *(digit_string + j);
} for (; k > ; i--, k--)
{
*(format_string + i) = '';
} if ( (*(format_string + i) == '.') )
{
i--;
*(format_string + i) = '';
} for (i--; i >= ; i--)
{
*(format_string + i) = ' ';
}
}
else
{
for (i = formatlen, j = digitlen; j >= ; i--, j--)
{
if ( (*(format_string + i) == ',')
|| (*(format_string + i) == '.') )
i--;
*(format_string + i) = *(digit_string + j);
}
for (; i >= ; i--)
*(format_string + i) = ' ';
}
} return ;
} int main (void)
{
char formats[] = "###,###.#####";
char const digits[] = ""; printf("%s\n", formats);
printf("%s\n", digits);
if (format(formats, digits))
printf("%s\n", formats);
else
printf("Data error!\n"); getchar();
return ;
}
9.17