不需要任何中文#include <stdio.h> #include <string.h> #define MAX_LEN 100 int main() { char s[MAX_LEN + 1]; char c; int len, i, j; int found = 0; printf("请输入一个字符串:"); scanf("%s", s); printf("请输入一个字符:"); scanf(" %c", &c); len = strlen(s); for (i = 0, j = 0; i < len; i++) { if (s[i] == c) { found = 1; continue; } s[j++] = s[i]; } s[j] = '\0'; if (found) { printf("删除 %c 后的字符串为:%s\n", c, s); } else { printf("NotFound\n"); } return 0; }用c语言编写,不需要中文!
时间: 2023-07-16 10:13:35 浏览: 41
以下是不需要中文的C代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char s[MAX_LEN + 1];
char c;
int len, i, j;
int found = 0;
printf("Please enter a string: ");
scanf("%s", s);
printf("Please enter a character: ");
scanf(" %c", &c);
len = strlen(s);
for (i = 0, j = 0; i < len; i++) {
if (s[i] == c) {
found = 1;
continue;
}
s[j++] = s[i];
}
s[j] = '\0';
if (found) {
printf("String after deleting %c: %s\n", c, s);
} else {
printf("NotFound\n");
}
return 0;
}
```
相关问题
#include <stdio.h> #include <stdlib.h> static char buff[256]; static char* string; int main() { printf("Please input your string:"); gets(string); printf("\nYour string is:%s\n",string); }
这段代码是一个简单的C程序,它的功能是从标准输入中读取一行字符串并输出。
然而,这段代码存在一个严重的问题:它使用了不安全的gets函数来读取输入,可能会导致缓冲区溢出漏洞。正确的做法是使用fgets函数来代替gets函数,限制读取的字符数量。
另外,这段代码声明了一个静态的字符数组buff,但是却没有使用它。而指针变量string却没有被初始化,使用时会导致未定义的行为。应该将string指向buff数组,或者使用动态内存分配函数malloc来为string分配内存。
改进后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 256
int main() {
char buff[MAX_LEN];
char *string = (char*)malloc(MAX_LEN * sizeof(char));
printf("Please input your string:");
fgets(buff, MAX_LEN, stdin);
sscanf(buff, "%s", string); // 去掉字符串末尾的换行符
printf("\nYour string is:%s\n", string);
free(string);
return 0;
}
```
请优化这段代码,执行ioctl报错无效的参数:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <arpa/inet.h> #include <linux/route.h> int main() { int fd; struct rtentry route; struct sockaddr_in *addr; memset(&route, 0, sizeof(route)); route.rt_dst.sa_family = AF_INET; addr = (struct sockaddr_in *)&route.rt_gateway; addr->sin_family = AF_INET; fd = socket(AF_INET, SOCK_DGRAM, 0); if (ioctl(fd, SIOCRTMSG, &route) == -1) { perror("ioctl"); exit(1); } close(fd); printf("Gateway address: %s\n", inet_ntoa(addr->sin_addr)); return 0; }
这段代码的问题在于使用了已经过时的 SIOCRTMSG ioctl 命令,并且没有对错误进行恰当的处理。可以使用更现代化的方法,如使用 netlink socket 来获取网关地址。以下是更新后的代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#define MAX_PAYLOAD 1024
int main() {
int sockfd;
struct sockaddr_nl sa;
struct nlmsghdr *nlh;
char buffer[MAX_PAYLOAD];
struct rtmsg *rtm;
struct rtattr *rta;
int len;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
nlh = (struct nlmsghdr *)buffer;
rtm = (struct rtmsg *)NLMSG_DATA(nlh);
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nlh->nlmsg_type = RTM_GETROUTE;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
sockfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sockfd == -1) {
perror("socket");
exit(1);
}
if (bind(sockfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
perror("bind");
exit(1);
}
if (send(sockfd, nlh, nlh->nlmsg_len, 0) == -1) {
perror("send");
exit(1);
}
while ((len = recv(sockfd, buffer, MAX_PAYLOAD, 0)) > 0) {
for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, len); nlh = NLMSG_NEXT(nlh, len)) {
if (nlh->nlmsg_type == NLMSG_DONE) {
break;
} else if (nlh->nlmsg_type == NLMSG_ERROR) {
perror("recv");
exit(1);
} else if (nlh->nlmsg_type == RTM_NEWROUTE) {
rtm = (struct rtmsg *)NLMSG_DATA(nlh);
if (rtm->rtm_table != RT_TABLE_MAIN) {
continue;
}
rta = (struct rtattr *)RTM_RTA(rtm);
len = RTM_PAYLOAD(nlh);
for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
if (rta->rta_type == RTA_GATEWAY) {
struct in_addr *addr = (struct in_addr *)RTA_DATA(rta);
printf("Gateway address: %s\n", inet_ntoa(*addr));
close(sockfd);
return 0;
}
}
}
}
}
close(sockfd);
return 0;
}
```
这个程序使用 netlink socket 发送 RTM_GETROUTE 请求来获取路由表,并遍历路由表中的每个条目,找到目标路由条目,并从中提取出网关地址。此方法可以避免使用已经过时的 ioctl 命令,并且可以正确地处理错误。