Help with C

+1 Justo Rodriguez · November 28, 2015
Hey I am not that familiar with C and I have an Assignment. What I am looking to do is I will enter ./filter Mary Alice <input_file
into the cmd and my code should

//read the text from the input file
//use strtok to separate words at the spaces
//use strcmp on each word
//copy the words into an output buffer
//Using strcmp, every time the first word is found, it will copy the second word to the output instead.
//print output

But I am stuck so far. I think my code is writing the delimiter to the output instead of the tokens. Anyone check my code and help? BTW I know my code is not finished. 

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

int main(int argc, char* argv[])
{
    int i = 0;
    int o = 0;
    char* fromStr = argv[1];
    char input[1024];
    char output[1024];
    char space[] = " ";
    char* token;

    token = strtok(input, space);

    if(fromStr == 0 )
        {
        printf("There isnt a from string \n");
        return 1;
        }

        char* toStr;

    if(argv[2] != 0)
        {
        toStr = argv[2];
        }
    else
        {
        toStr = "";
        }

while(token != NULL)
        {
               //input = output[o];
                strcpy(output, token);

                if(strcmp(fromStr, token))
                {
                        strcpy(token, toStr);
                }

                printf("%s \n", output);
                token = strtok(NULL, space);

        }

    while(fgets(input,sizeof(input),stdin))
    {
                printf("%s", input);


       
    }
    return 0;
}



Post a Reply

Replies

Oldest  Newest  Rating
0 c student · November 29, 2015
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
   int n = 0;
   int o = 0;
   char* fromStr = argv[1];
   char input[1024];
   char output[1024];
   char space[] = " ";
   char* token;

   token = strtok(input, space);

   if(fromStr == 0)
   {
       printf("There isnt a from string \n");
       return 1;
   }

   char* toStr;

   if(argv[2] != 0)
   {
       toStr = argv[2];
   }
   else
   {
       toStr = "";
   }

   while(token != NULL)
   {
       //input[n] = output[o];
       strcpy(output, token);

       if(strcmp(fromStr, token))
       {
           strcpy(token, toStr);
       }
       printf("%s \n", output);
       token = strtok(NULL, space);
   }

   while(fgets(input,sizeof(input),stdin))
   {
       printf("%s", input);
   }

   return 0;
}

not sure where you put your i indexes but i replaced it with n so it doesn't interfere with the italics code.
0 Justo Rodriguez · November 29, 2015
I tried your code and it doesn't output the changed text. It outputs this instead

bingsuns2% ./filter Mary Alice < input_file
???
Mary had a little lamb
Its fleece was white as snow
and everywhere that Mary went
the lamb was sure to go
0 c student · November 29, 2015
of course it doesn't work...  i just made it look cleaner so people are more willing to help you.  you're welcome by the way.
+1 Dan P · November 29, 2015
Hi Justo,

In your while loop you

1. Copy token to output.   - Mary
2. Check if token is fromStr.  -  Mary equals Mary
3. Set token to toStr.   -  token is Alice
4. Print output.   -  output is Mary
5. Get the next token.   - move on...

You don't set output to Alice anywhere...

Dan
0 c student · November 30, 2015
the main issue i'm concerned with is that you'll somehow end up with a segmentation fault or that somehow your buffer won't be big enough to fit in the extra letters you're adding in.  you also seem to be confusing yourself with what you need to achieve.  know what steps are required and design a proper psuedo algorithm and then try implementing small code and gradually build from there.

i have developed my own code which reads in from stdin and replaces each encountered string with another.  you may take a look as reference or copy it if you wish but it will provide absolutely no help to you if you do not understand it.

Spoiler:

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

// pointer to struct node
typedef struct node *NodePtr;

// node to hold string
struct node {
    char *string;
    NodePtr next;
};

// create new node
NodePtr initNode (char *s) {
    NodePtr n = malloc (sizeof (struct node));
    assert (n != NULL);
    
    n->string = s;
    n->next = NULL;

    return n;
}

// free alloc memory
void freeList (NodePtr h) {
    NodePtr curr;
    while ((curr = h) != NULL) {
        h = h->next;
        free (curr);
    }
}

int main (void) {
    char input[BUFSIZ];
    char *token = NULL;
    char output[BUFSIZ*2];    // double size for extra space

   // zero memory in buffers
    memset (input, 0, sizeof (input));
    memset (output, 0, sizeof (output));
    
   // read from stdin
    fgets (input, sizeof (input), stdin);

   // separate strings
    token = strtok (input, " ");
   // put string into a node
    NodePtr head = initNode (token);
    NodePtr current = head;

    while (token != NULL) {
       // get next string
        token = strtok (NULL, " ");
       // create new node to hold next string
        current->next = initNode (token);
       // check current node for target string
        if (current->string != NULL) {
            if (strncmp (current->string, "foo", strlen (current->string)) == 0) {
                // replace string
               current->string = "bar";
            }
        }
       // go to next node
        current = current->next;
    }

   // place each string back into a buffer
    for (current = head; current->next != NULL; current = current->next) {
        snprintf (output+strlen (output), sizeof (output) - strlen (output), "%s ", current->string);
    }

   // print result
    printf ("%s\n", output);

   // free node memory
    freeList (head);

    return EXIT_SUCCESS;
}



http://ideone.com/xmQXSH
0 Justo Rodriguez · November 30, 2015
Do I use strcpy to set output to Alice?
+1 Dan P · November 30, 2015
Hi Justo,

Just re-reading your code, and the design doesn't make any sense.

I suggest you read the code that c student has kindly made and understand what that does.

Although, the code that c student has provided will not work.

Dan
  • 1

C

106,953 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator