gpt4 book ai didi

c - 指针和cstring初学者的烦恼

转载 作者:行者123 更新时间:2023-11-30 16:03:50 25 4
gpt4 key购买 nike

我的replace函数有问题。我想要完成的是替换一些特殊字符,但我还没有写代码。因此,在 replace 函数中,我们此时可以说该函数应该按照我尝试编写的方式逐行打印。有人可以纠正这个功能吗?我实在看不懂

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct list_el {
char *ord;
int num;
struct list_el *prev;
struct list_el *next;
};

typedef struct list_el item;
struct list_el *head, *tail; /*Double linked list that makes it easier to add a element to the end of the FIFO list*/

void addNode(struct list_el *curr);
void readFile();
void print();
void replace();
void random();
void len();

int antE = 0;

int randint(int max)
{
int a = (max*rand()/(RAND_MAX+1.0));
return a;
}

int main(int argc, char *argv[]) {
item *curr;

struct list_el *pa;

if(argc == 3) {
readFile();
}
if(argc == 1) {
printf("Too few arguments, must bee 3");
} else if(strcmp(argv[1], "print") == 0) {
print();
} else if(strcmp(argv[1], "random") == 0) {
random();
} else if(strcmp(argv[1], "replace") == 0) {
replace();
} else if(strcmp(argv[1], "remove") == 0) {
printf("Random kommando kalt");
} else if(strcmp(argv[1], "len") == 0) {
len();
} else {
printf("Not a valid command");
}

if(argc == 3) {
free(curr);
}
}

void addNode(struct list_el *curr) {
if(head == NULL) {
head = curr;
curr->prev = NULL;
} else {
tail->next = curr;
curr->prev = tail;
}

tail = curr;
curr->next = NULL;
}

void readFile()
{
FILE *f = fopen("tresmaa.txt", "r");

if(f == 0) {
printf("Could not open file");
exit(8);
}
item *curr;

if(f != NULL) {
int antE = 0;
head = NULL;
char buffer[300];

while(fgets(buffer, 300-1,f) != NULL) {
curr = (item*)malloc(sizeof(item));
curr->ord = malloc(300);
curr->num = antE;
strcpy(curr->ord, buffer);
antE++;
addNode(curr);
}
}
fclose(f);
}

/*Traverserer listen og printer ut linje for lije
*/
void print()
{
item *curr;
printf("Print text:\n");
for(curr = head; curr != NULL; curr = curr->next) {
printf("%s", curr->ord);
}
}

/*Printer ut en tilfeldig setning
*/
void random()
{
item *curr;
int anum = randint(antE);
for(curr = head; curr != NULL; curr = curr->next) {
if(curr->num == anum) {
printf("Print a random line:\n%s", curr->ord);
}
}
}

void replace()
{
item *curr;
int i;
char tmp[300];

printf("Replace vowels ...\n");
printf("... with vowel 'a'\n");

for(curr = head; curr != NULL; curr = curr->next) {
strcpy(tmp, curr->ord);

for(i = 0; i < strlen(tmp); i++) {
printf("%s", tmp[i]);
}
}
}

void len()
{
item *curr;
long nc;
int i;
nc = 0;

for(curr = head; curr != NULL; curr = curr->next) {
nc += strlen(curr->ord);
}
printf("The text is %d characters long", nc);
}

最佳答案

如果您只想打印这些行,则无需复制和额外的循环即可完成:

for(curr = head; curr != NULL; curr = curr->next) {
printf("%s\n", curr->ord);
}

您当前的代码不起作用,因为您使用 %s 格式告诉 printf 它的参数将是一个字符串(也称为指向以零结尾的指针)字符序列),但是你给它一个字符,而不是这样的指针。

关于c - 指针和cstring初学者的烦恼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3743466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com