c语言编程问题(不能用itoe和gets库函数)

2025-02-25 23:49:08
推荐回答(1个)
回答1:

这个问题其实就是对字符的操作,首先判断输入的字符串长度是否为4的倍数,如果不是那么就可以判读No了,如果是在分别去四个字符去判断是否是2019,然后就完了。代码如下:

#include 
#include 

#define MAX_LENGTH ( 80 )

void my_strncpy(char *dest, char const *src) {

int size = 4;
while ( size-- ) {

*dest++ = *src++;
}
*dest = '\0';
}

int main(void) {

char str[MAX_LENGTH+1];
char str_buff[5];
char *pStr;
char ch = '#';
int n = 0;
int i, j;

printf("请输入: ");
while ( ch != '\n' && ch != '\r' && n < MAX_LENGTH ) {

ch = getchar();
str[n++] = ch;
}
str[n] = '\0';
/*
 *   如果字符串不是4的倍数,那么肯定不是2019组成的
 */
if ( ( n - 1 ) % 4 != 0 ) {

printf("No\n");
return 0;
}
/*
 *  开始判断是否由2019组成的
 */
pStr = str;
for ( i = 0; i < ( ( n - 1 ) / 4 ); i++ ) {

my_strncpy(str_buff, pStr);
if ( strcmp(str_buff, "2019") != 0 ) {

printf("No\n");
return 0;
}
pStr += 4;
}
printf("Yes\n");
return 0;
}

不知道这样符不符合你的问题,满意的话麻烦点个采纳,谢谢!