Thread: Help needed!

  1. #1
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82

    Help needed!

    I started my c lessons 2 weeks ago and i have a project to do. This project is quite difficult for a beginner like me. I have to make a program that receives an arbitrary number of words and a text, searches those words in the text, and then returns every line where it found at least one of the words. It should work similary to this: promp> program.c word1 word2 …wordn < text.txt. The program cannot manipulate files, due to the “< text.txt” thing , it has to work as if the text was written in the command line. As obvious, I thought in many solutions for this problem, but I´m having a lot of trouble implementing them. I think that the best solution(since i´m supposed to use getchar() and putchar() everywhere) would be to read char by char and then write every char in a vector until it gets to the end of the line. After that, it compares the words received in the command line with those in the vector. If it has at least one match, a counter is incremented, and if the counter is > 0,the line is shown as expected. It seems logical, but i´m having a lot of trouble implementing this solution. If someone can help me, give me some hints, or even give me some idea of a possible solution, it would be really helpful. Hope someone can help me!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C doesn't have vectors, so you'll have to do something else. Since it's an apparent requirement that you use getchar(), I'd suggest reading into a buffer until you reach a newline (or the end of the buffer), and then stick all of that into a linked list.

    Add to the list until you have no more input. Then, go through each list entry looking for your word or prhase. You can use strstr to search strings for substrings.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    By vector i meant an array, i´m sorry if I wasnt very clear, but in portuguese they are all the same thing. So,ok, i managed to "write" the current line in an array, how do i compare the words received with those in the array?as far as I know, i´ll be comparing chars (since in the array there are chars) with words, received in the command line, how do i do that?I know i have to work with argv and argc,that´s not problem,the problem is comparing things and then go to the next line. And at the EOF, i also want to read the current line, but i cant detect the newline char, right?

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Lost_Soul, well, your problem is comparing or what? look, for comparing simple chars (not array of chars, strings) you can do like this:
    Code:
    if (myChar == 'y')
    Now if you want to compare strings (array of chars) you can use the
    strcmp() checkout the return value of the function to don't get confused.


    Ahh, fala portugues? portugal or brasil?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>then stick all of that into a linked list.
    No point in a linked list, imho. Only one line is needed in memory at a time (to scan for specific words), so no need to over complicate things.

    >>you can use the strcmp() ...
    You can, but not for this task. You'll need strstr() as mentioned by Quzah, to find a string within a string.

    Lost__Soul, have a look at this snippet:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
      char a[] = "Try to find me";
      char *p;
      
      p = strstr(a, "find");
      if (p)
        puts ("Found it!");
      
      return 0;
    }
    Lookup about strstr() in the link provided by Quzah to help you understand what is going on here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well Hammer I told strcmp() because he wants to compare strings, if I understand right.

    I didn't tell this will solve his problem, I was just showing how to compare strings or chars.

    Look what I wrote:
    IF you want...

    Please hammer, you took just 1/3 of my sentence, and my full sentence was:
    if you want to compare strings (array of chars) you can use the
    strcmp()
    And not just you can use strcmp()
    Last edited by Vber; 03-18-2003 at 07:16 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Hey, Vber, don't go all defensive on me

    >>I told strcmp() because he wants to compare strings, if I understand right.
    They want to find a string within a string, and strcmp() can't do that, so it's off topic really. Hence the strstr() function was mentioned.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Well,i read every message and i have to thank you all for the help u´re giving me. The problem is that i cant use string commands or pointers.I know this seems strange,but i´m suposed to work only with getchar,putchar. The only thing i need to know right now is how to compare the words in the comand line with those in the text lines. As i said, the program reads every char,stores them in an array, and when it finds the '\n' it goes to the line evaluation process. What i really need to know is how to compare without having strstr. My thought is to use a function that works exactly (or similary) as strstr (can anyone post the strstr function here?),that way, i wouldn´t be using any string command,and i could compare the words.Another thing:in the command line,i´ll have strings,each word is a string,and in the array,i´ll have chars,but i guess the array itself will be a big string with strings in the inside,right?so i can search for the words in the array using something similar to strstr?But doesnt the strstr function return the first ocorrence of the string in the "main" string?I´m asking this because i have to count all the ocorrences of every word i´m looking for,so i cant stop at the first match. Another problem is when i have to read the next line. I´ll have to call the function that writes the line in the array,but how can i call it with the array?(i know this is quite a silly question,but believe me,in 2 weeks u dont learn almost anything,so they didnt teach us those details). For example, i have line[]... and then i need to call the array named line.

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Yes, Now I readed the text again, and I understand a little bit better, well, my english isn't good enough to understand at the first time

    >>Hey, Vber, don't go all defensive on me
    Oh yes yes, sorry man, I was a little bit nervous.

    >>They want to find a string within a string, and strcmp() can't do that
    I tought he was trying to compare strings, just compare the and not to find a string within a string

  10. #10
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Any idea...?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Lost__Soul
    Any idea...?
    People will provide ideas when they have them, please don't bump your posts. I do have some things to say, but don't have the time to write them at present.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Just asking...I thought this problem was easy to solve...Since I´m suposed to do it in a week and i never worked with c before...
    Just getting a little desperate,nothing serious

  13. #13
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Ok,i have an idea,can someone please post here the description(code) of the function strstr? For code i mean that,for example, the function strcountc has the folowig code:

    int strcountc (char *s, char ch)
    {
    ....
    }
    Can someone poste the strstr one?Dont know exactly if the name in english is "code"...

  14. #14
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    You mean the prototype of strstr()? well
    Code:
    char *strstr(const char *s1, const char *s2);
    And more info can be found here

    Afinal, se fala portugues ou nao?

  15. #15
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Sim,falo portugues,mas para alterar a funçao de acordo com o que preciso,tenho de ter mais que o protótipo,não achas?eu preciso do código da funçao para o poder alterar,ou seja,se quisesse escrever esta função em c,como faria?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM