#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
// 保存各个种类的字符的数量,
int iCtrl_Count = 0; // 控制字符
int iBlack_Count = 0; // 空白字符
int iNum_Count = 0; // 数字
int iSmall_Count = 0; // 小写字母
int iBig_Count = 0; // 大写字母
int iPunctu_Count = 0; // 标点符号
int iNotPut_Count = 0; // 不可打印的字符
// 两种方式:1.直接gets字符串,然后计算;2.通过getchar一个一个获取字符,然后计算(EOF控制台好像用不了)
char chArray[1000];
gets(chArray);
int iCount = 0;
while (*(chArray + iCount ) != '\000')
{
if( iscntrl( *(chArray + iCount )) )
++iCtrl_Count;
if( isspace( *(chArray + iCount ) ) )
++iBlack_Count;
if( isdigit( *(chArray + iCount ) ) )
++iNum_Count;
if( islower( *(chArray + iCount ) ) )
++iSmall_Count;
if( isupper( *(chArray + iCount ) ) )
++iBig_Count;
if( ispunct( *(chArray + iCount ) ) )
++iPunctu_Count;
if( !isprint( *(chArray + iCount ) ) )
++iNotPut_Count;
++iCount;
}
printf("Ctrl_Count = %f\n", (float)iCtrl_Count / (float)iCount);
printf("Black_Count = %f\n", (float)iBlack_Count / (float)iCount);
printf("Num_Count = %f\n", (float)iNum_Count / (float)iCount);
printf("Small_Count = %f\n", (float)iSmall_Count / (float)iCount);
printf("Big_Count = %f\n", (float)iBig_Count / (float)iCount);
printf("Punctu_Count = %f\n", (float)iPunctu_Count / (float)iCount);
printf("NotPut_Count = %f\n", (float)iNotPut_Count / (float)iCount);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int my_strlen(char *pchStr, int iSize)
{
int iLen = 0;
for (iLen = 0; iLen < iSize; ++iLen)
{
if (*pchStr++ == '\000')
break;
}
return iLen;
}
int main()
{
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
/*
函数原型: char *my_strcpy(char *dest, const char *src) //将src复制到dest字符数组中
返 回 值:char* 类型,返回的是第一个参数的值,即目的数组的首地址;
*/
// 数组未初始化的话,数组内的数据是不确定的,所以无法判断数组的长度,所以只能外面给定数组的长度
char *my_strcpy(char *dest, const char *src, int iLen)
{
int i = 0;
while ( *(src + i) != '\000')
{
*(dest + i) = *(src + i);
++i;
if (i >= iLen)
{
*(dest + i) = '\000';
break;
}
}
if (i < iLen)
{
*(dest + i) = '\000';
}
return dest;
}
int main()
{
char chSrc[5] = {'1','2','3','4','5'};
char chSest[3];
my_strcpy(chSest, chSrc, 3);
puts(chSest);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
/*
函数原型: char *my_strcat(char *_Destination,const char *_Source) //将src复制到dest字符数组中
返 回 值:char* 类型,返回的是第一个参数的值,即目的数组的首地址;
*/
// 数组未初始化的话,数组内的数据是不确定的,所以无法判断数组的长度,所以只能外面给定数组的长度
char *my_strcat(char *dest, const char *src, int iDestLen)
{
// 不做入参检查,非法操作会产生段错误
int i = 0;
for (i = 0; i < iDestLen; ++i)
{
if (*(dest + i) == '\000')
break;
}
int j = 0;
while ( *(src + j) != '\000')
{
char ch = *(src + j);
*(dest + i) = *(src + j);
++i;
++j;
if (i >= iDestLen)
{
*(dest + i) = '\000';
break;
}
}
if (i < iDestLen)
{
*(dest + i) = '\000';
}
return dest;
}
int main()
{
char chSrc[5] = {'1','2','3','4'}; // 如果5个字符填满,那么第六个字符不一定是\000, 如果5个字符没有填满,那么最后一个字符后面一定是\000
char chSest[8] = {'8','7'};
my_strcat(chSest, chSrc, 8);
puts(chSest);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
/*
函数原型: char *my_strcpy_end(char *dest, const char *src) //将src复制到dest字符数组中
返 回 值:char* 类型,返回一个指向目标字符串末尾的指针(也就是说,指向NUL字节的指针)
*/
// 数组未初始化的话,数组内的数据是不确定的,所以无法判断数组的长度,所以只能外面给定数组的长度
char *my_strcpy_end(char *dest, const char *src, int iLen)
{
int i = 0;
while ( *(src + i) != '\000')
{
*(dest + i) = *(src + i);
++i;
if (i >= iLen)
{
*(dest + i) = '\000';
break;
}
}
if (i < iLen)
{
*(dest + i) = '\000';
}
return dest + i;
}
int main()
{
char chSrc[5] = {'1','2','3','4','5'};
char chSest[3];
my_strcpy_end(chSest, chSrc, 3);
puts(chSest);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
char *my_strrchr( char const *str, int ch )
{
int i = 0;
int iPosi = 0;
while (*(str + i) != '\000')
{
if(*(str + i) == ch)
iPosi = i;
++i;
}
return str + iPosi;
}
int main()
{
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
char *my_strnchr( char const *str, int ch, int which )
{
if (which <= 0)
return NULL;
int i = 0;
int iPosi = 0;
int iFrequency = 0;
while (*(str + i) != '\000')
{
if(*(str + i) == ch)
{
iPosi = i;
++iFrequency;
if (iFrequency == which)
break;
}
++i;
}
if (iFrequency < which)
return NULL;
return str + iPosi;
}
int main()
{
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int count_chars( char const *str, char const *chars )
{
int i = 0;
int iMateCount = 0;
while (*(chars + i) != '\000')
{
int j = 0;
while (*(str + j) != '\000')
{
if (*(str + j) == *(chars + i))
{
++iMateCount;
break;
}
++j;
}
++i;
}
return iMateCount;
}
int main()
{
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int palindrome( char *string )
{
int iRet = 1;
int iLen = 0;
while (*(string + iLen) != '\000') // 得到字符串的长度
{
if (isalpha(*(string + iLen)) == 0)// 判断字母
continue;
++iLen;
}
char *pchArray = (char *)malloc(sizeof(char) * iLen);
iLen = 0;
int iChArrayLen = 0;
while (*(string + iLen) != '\000')
{
if (isalpha(*(string + iLen)) != 0)
{
*(pchArray + iChArrayLen) = *(string + iLen);
++iChArrayLen;
}
++iLen;
}
// 0, iLen-1
for (int i = 0; i < iChArrayLen; ++i)
{
if (*(pchArray + i) != *(pchArray + iChArrayLen - 1 - i))
{
iRet = 0;
break;
}
}
return iRet;
}
int main()
{
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
static const char *st_pchThe = "the";
int GetCount(char *pchSrc)
{
int i = 0;
int iCount = 0;
while (*(pchSrc + i) != '\000' && *(pchSrc + i + 1) != '\000' && *(pchSrc + i + 2) != '\000')
{
if (*(pchSrc + i) == *st_pchThe && *(pchSrc + i + 1) == *(st_pchThe + 1) &&*(pchSrc + i + 2) == *(st_pchThe + 2)) // 碰到t
{
++iCount;
}
++i;
}
return iCount;
}
int main()
{
char *pchArr = "thetheTheTHEth";
printf("%d\n",GetCount(pchArr));
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void Init26Char(char *pchArr)
{
int i = 0;
for (i = 0; i < 26; ++i)
{
*(pchArr + i) = 'A' + i;
}
*(pchArr + i) = '\000';
}
// 不去分大小写,要么全是大写,要么全是小写, 这里只分辨大写的
int prepare_key( char *key ) // key的长度 > 26 ,入参key是一个秘钥单词,出参key是一个26长度的秘钥数组
{
if (*key == '\000') // 判空
return 0;
// 转成大写字母
int iKeyOffset = 0;
while (*(key + iKeyOffset) != '\000')
{
*(key + iKeyOffset) = toupper(*(key + iKeyOffset));
++iKeyOffset;
}
// 只保留最早的出现一次的字母到chFlag
char chFlag[27] = {0};
iKeyOffset = 0; // 作为key 的偏移量
int iFlagOffset = 0; // 作为Flag 的偏移量
while(*(key + iKeyOffset) != '\000')
{
if (0 == isalpha(*(key + iKeyOffset)))
return 0;
int k = 0;
while (*(chFlag + k) != '\000' && *(chFlag + k) != *(key + iKeyOffset)) // 遍历chFlag没有相同的字母
{
++k;
}
if (*(chFlag + k) == '\000')
{
*(chFlag + iFlagOffset) = *(key + iKeyOffset);
++iFlagOffset;
}
++iKeyOffset;
}
// 用'\000'替换掉chArr中chFlag的部分
--iFlagOffset;
char chArr[27];
Init26Char(chArr); // 得到ABCD顺序的26个字母
for (;iFlagOffset >= 0; --iFlagOffset)
{
for (int n = 0; n < 26; ++n)
{
if (*(chFlag + iFlagOffset) == *(chArr + n))
{
*(chArr + n) = '\000';
break;
}
}
}
// 填满chFlag
iFlagOffset = strlen(chFlag);
for (int n = 0; n < 26; ++n)
{
if (*(chArr + n) == '\000')
continue;
*(chFlag + iFlagOffset) = *(chArr + n);
++iFlagOffset;
}
strcpy(key,chFlag);
return 1;
}
void encrypt( char *data, char const *key )
{
if (!prepare_key( key ))
return;
int iDataOffset = 0;
while (*(data + iDataOffset) != '\000') // 将data转换成大写
{
*(data + iDataOffset) = toupper(*(data + iDataOffset));
++iDataOffset;
}
char chArr[27];
Init26Char(chArr); // 得到ABCD顺序的26个字母
iDataOffset = 0;
while (*(data + iDataOffset) != '\000')
{
int i = 0;
while (*(chArr + i) != '\000')
{
if (*(chArr + i) == *(data + iDataOffset)) // 拿到data的各个元素在ABCD数组的位置
{
*(data + iDataOffset) = *(key + i); // 将key种对应位置的参数赋值到data
break;
}
++i;
}
++iDataOffset;
}
}
void decrypt( char *data, char const *key )
{
if (!prepare_key( key ))
return;
int iDataOffset = 0;
while (*(data + iDataOffset) != '\000') // 将data转换成大写
{
toupper(*(data + iDataOffset));
++iDataOffset;
}
char chArr[27];
Init26Char(chArr); // 得到ABCD顺序的26个字母
iDataOffset = 0;
while (*(data + iDataOffset) != '\000')
{
int i = 0;
while (*(key + i) != '\000')
{
if (*(key + i) == *(data + iDataOffset))
{
*(data + iDataOffset) = *(chArr + i);
break;
}
++i;
}
++iDataOffset;
}
}
int main()
{
// 12题的验证
#if 0
char chArray[27] = "HELEOLOPH";
prepare_key( chArray );
puts(chArray);
#endif
// 13题的验证
char chkey[27] = "HELEOLOPH";
char chData[27] = "HelloWorld";
encrypt( chData, chkey );
puts(chData);
// 14题的验证
decrypt( chData, chkey );
puts(chData);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
void dollars( char *dest, char const *src )
{
// *src在这里表示整型数据
int iDestOff = 0;
*(dest + iDestOff)= '$';
++iDestOff;
int iSrcOff = 0;
while (*(src + iSrcOff) != '\000') // 拿到字符串的长度,其实这种方式和使用strlen()函数的效果是一样的
{
++iSrcOff;
}
int i = 0;
if (iSrcOff > 2)
{
int iCount = iSrcOff - 2; // 最后两位在小数点后面
while (iCount > 0)
{
if (iCount % 3 == 0)
{
*(dest + iDestOff) = ','; // 判断是否要添加逗号
++iDestOff;
}
*(dest + iDestOff) = *(src + i);
++iDestOff;
++i;
--iCount;
}
// 判断是否要添加点
}
else
{
*(dest + iDestOff) = '0';
++iDestOff;
}
*(dest + iDestOff) = '.';
++iDestOff;
*(dest + iDestOff) = (iSrcOff == 1) ? '0' : *(src + i++);
++iDestOff;
*(dest + iDestOff) = (iSrcOff == 1) ? *src : *(src + i++);
++iDestOff;
*(dest + iDestOff) = '\000';
}
int main()
{
char chSrc[10] = "123456";
char chDest[10];
dollars( chDest, chSrc );
printf("%s\n", chDest);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int format( char *format_string, char const *digit_string )
{
int iFormatLen = 0;
int iOff = 0;
while (*(format_string + iOff) != '\000')
{
if (*(format_string + iOff) == '#')
++iFormatLen;
++iOff;
}
int iDigitLen = strlen(digit_string);
if (iDigitLen < 1 || iFormatLen < iDigitLen)
return 0;
int iOff_Digit = 0;
char *pchArr = (char *)malloc(sizeof(char) * iOff);
// 可以通过压栈的方式插入,栈的长度为iOff
for (int i = 1; i < iOff + 1; ++i)
{
if (*(format_string + iOff - i) == '#')
{
++iOff_Digit;
if ( iDigitLen >= iOff_Digit && isdigit( *(digit_string + iDigitLen - iOff_Digit) ) )
*(pchArr + iOff - i) = *(digit_string + iDigitLen - iOff_Digit);
else
*(pchArr + iOff - i) = ' ';
}
else if (*(format_string + iOff - i) == ',' || *(format_string + iOff - i) == '.')
{
if (iDigitLen >= iOff_Digit)
*(pchArr + iOff - i) = *(format_string + iOff - i);
else
*(pchArr + iOff - i) = ' ';
}
}
*(pchArr + iOff) = '\000';
strcpy(digit_string, pchArr);
return 1;
}
int main()
{
char chFormat[15] = "#,###,###.##";
char chDigit[15] = "123456789";
if (format( chFormat, chDigit))
printf("%s\n", chDigit);
char chDigit2[15] = "345";
if (format( chFormat, chDigit2))
printf("%s\n", chDigit2);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *edit(char *pattern, char const *digits) // 这个代码是抄别人的
{
char fullch;
int signif = 0;
char *sign = NULL;
if (pattern == NULL || digits == NULL)
return NULL;
fullch = *pattern;
while (*pattern != '\0') {
switch (*pattern) {
case '#':
if (*digits == '\0') {
*pattern = *digits;
return sign;
}
else if (signif == 0) {
if (*digits == '0' || *digits == ' ')
*pattern = fullch;
else {
*pattern = (*digits == ' ')? '0' : *digits;
signif = 1;
sign = pattern;
}
}
else {
*pattern = (*digits == ' ')? '0' : *digits;
}
digits++;
break;
case '!':
if (*digits == '\0') {
*pattern = *digits;
return sign;
}
else if (signif == 0) {
*pattern = (*digits == ' ')? '0' : *digits;
signif = 1;
sign = pattern;
}
else {
*pattern = (*digits == ' ')? '0' : *digits;
}
digits++;
break;
default:
if (signif == 0)
*pattern = fullch;
}
pattern++;
}
return sign;
}
int main()
{
char chFormat[15] = "#,###,###.##";
char chDigit[15] = "123456789";
if (format( chFormat, chDigit))
printf("%s\n", chDigit);
char chDigit2[15] = "345";
if (format( chFormat, chDigit2))
printf("%s\n", chDigit2);
system("pause");
return EXIT_SUCCESS;
}