could you please finish it without using <string.h>, <ctype.h> or <stdbool.h>
时间: 2023-09-11 08:05:49 浏览: 106
302-字符函数ctype.h应用举例(51单片机C语言实例Proteus仿真和代码)
Sure, here's the modified code without using `<string.h>`, `<ctype.h>`, or `<stdbool.h>`:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_WORD_SIZE 80
#define ASCII_SIZE 128
void print_colored(char* text, char* color) {
printf("\033[%sm%s\033[0m", color, text);
}
int strlen(char* str) {
int len = 0;
while (*str != '\0') {
len++;
str++;
}
return len;
}
int isalpha(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
int isdigit(int c) {
return c >= '0' && c <= '9';
}
int isspace(int c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
}
int strcmp(char* str1, char* str2) {
while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2) {
str1++;
str2++;
}
return *str1 - *str2;
}
char* strstr(char* haystack, char* needle) {
int needle_len = strlen(needle);
if (needle_len == 0) {
return haystack;
}
while (*haystack != '\0') {
if (*haystack == *needle) {
char* h = haystack;
char* n = needle;
while (*h != '\0' && *n != '\0' && *h == *n) {
h++;
n++;
}
if (*n == '\0') {
return haystack;
}
}
haystack++;
}
return NULL;
}
void decode(char* encoded_text, int n, char* decoded_text) {
int i = 0;
while (*encoded_text != '\0') {
decoded_text[i] = ((*encoded_text - n) % ASCII_SIZE + ASCII_SIZE) % ASCII_SIZE;
encoded_text++;
i++;
}
decoded_text[i] = '\0';
}
int main() {
char word[MAX_WORD_SIZE + 1];
int n;
char encoded_text[ASCII_SIZE];
fgets(word, MAX_WORD_SIZE + 1, stdin);
scanf("%d", &n);
getchar(); // read the newline character after n
int i = 0;
char c;
while ((c = getchar()) != EOF && c != '\n') {
encoded_text[i] = c;
i++;
}
encoded_text[i] = '\0';
char decoded_text[ASCII_SIZE];
decode(encoded_text, n, decoded_text);
if (strstr(decoded_text, word) != NULL) {
print_colored("YES\n", "32"); // green color
} else {
print_colored("NO\n", "31"); // red color
}
return 0;
}
```
I had to define some of the missing functions (`strlen()`, `isalpha()`, `isdigit()`, `isspace()`, `strcmp()`, and `strstr()`) using basic string manipulation techniques, since they are not available in the standard C library. The `decode()` function was modified to fill the `decoded_text` buffer directly, without returning a value.
I hope this meets your requirements! Let me know if you have any questions.
阅读全文