added cls05

This commit is contained in:
2026-05-25 10:54:41 +09:00
parent bddcc77361
commit 494e7b51bb
10 changed files with 179 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
include ../common.mk
PROJECT_NAME:=cls05
TGTS:=$(patsubst %.c,%,$(wildcard *.c))
all: $(TGTS)
%: %.c
@mkdir -p $(BUILD_PATH)/$(PROJECT_NAME)
$(CC) $(CFLAGS) -I. $^ -o $(BUILD_PATH)/$(PROJECT_NAME)/$@
clean:
$(RM) -drf $(BUILD_PATH)/$(PROJECT_NAME)
+38
View File
@@ -0,0 +1,38 @@
#include <stdio.h>
#define BUFF_SZ 256
int str_char(const char s[], int c) {
int idx = 0;
while (s[idx] != '\0') {
if (s[idx] == (char)c) {
return idx + 1;
}
idx++;
}
return -1;
}
int main(void) {
char buff[BUFF_SZ] = {0};
char query = 0;
printf("Input String: ");
fgets(buff, BUFF_SZ, stdin);
printf("Input Search Character: ");
scanf("%c", &query);
printf("%c, %s\n", query, buff);
int ret = str_char(buff, query);
if (ret != -1) {
printf("Found %c at %d\n", query, ret);
} else {
printf("Queried character not found.\n");
}
return 0;
}
+25
View File
@@ -0,0 +1,25 @@
#include <stdio.h>
#define BUFF_SZ 256
int main(void) {
char buff[BUFF_SZ] = {0};
int cnt = 0;
printf("Input String: ");
while ((buff[cnt] = getchar()) != '\n') {
cnt++;
};
buff[cnt] = '\0';
int i = 0;
while (buff[i] != '\0') {
printf(
"Char: %c, ASCII Code: 0x%X (%d)\n",
buff[i], (int)buff[i], (int)buff[i]
);
i++;
}
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#define BUFF_SZ 256
void rev_string(char s[]) {
int str_sz = 0;
char tmp = 0;
do {
str_sz++;
} while (s[str_sz] != '\0' && str_sz < BUFF_SZ);
str_sz--; // remove null terminater from count
for (int i = 0; i < str_sz/2; i++) {
tmp = s[str_sz - 1 - i];
s[str_sz - 1 - i] = s[i];
s[i] = tmp;
}
}
int main(void) {
char buff[BUFF_SZ] = {0};
printf("Input String: ");
fgets(buff, BUFF_SZ, stdin);
printf("Got: %s\n", buff);
rev_string(buff);
printf("Rev: %s\n", buff);
return 0;
}