Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1098677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:36:52+00:00 2026-05-17T00:36:52+00:00

I have a problem with the function replace . What I want to accomplish

  • 0

I have a problem with the function replace. What I want to accomplish is to replace some special characters, but I haven’t written the code yet. So in the replace function we can at this moment just say that the function should print line for line the way I have tried to write. Can someone please correct this function? I can’t really get it

#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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:36:53+00:00Added an answer on May 17, 2026 at 12:36 am

    If you just want to print the lines you can do it without the copying and extra loop:

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

    Your current code doesn’t work because you tell printf with the %s format that its argument will be a string (aka. a pointer to a zero-terminated sequence of characters), but then you give it a single character, not such a pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.