Thread: Help needed!

  1. #16
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hmm if you want to get answers faster don't write portuguese cuz besides me, just some people here know's, because I understood your problem, you can try to search here .

    Hmm nao entendi direito o que voce quer, se quer o codigo mesmo dela? bom isso eu nao sei, mas a funcao que eu criei la da para quebrar o galho sim...


  2. #17
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    I heard there´s a linux command called grep that does exactly what i want. Can anyone post here the grep source code in c??It would solve the problem,i guess

  3. #18
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Search google, you'll find, maybe try this

  4. #19
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    After a lot of work i managed to write a possible solution. The program seems to be ok,i´ve tried function by function and they worked,but some error is ocurring.I´m writing my program in eclipse and dont know exactly how to do the debugging process with a program that receives arguments in the command line. So,i´m posting the code here so that someone can detect the errors and possible tell me how to correct them.I hope i dont have to change a lot of things.By the way,its obvious i could use another functions that would make the program less complicated,but since i´m supposed to work only with what i learned,this is the most i could do...Sorry for the portuguese names for the variables,i tried to include some commentarys to make the understanding of the program easier.Hope someone can help me!


    #include <stdio.h>

    #define MAX_LINHA 128

    /* Compares the words received in the command line with those
    * in the array, char by char (the program must detect a word as
    * a subsequence of another word, for example, "um" is a subsequence
    * of "uma"). Returns the counter.
    */
    int comparaux(int n, char palavra[], char linha[], int pos_linha, int pos_palavra)
    {
    int j=0, p=pos_palavra, m=pos_linha;
    while(linha[m]!='\n')
    {
    if(palavra[p] == '\0')
    {
    n=n+1;
    comparaux(n, palavra, linha, m, 0);
    }
    else
    {
    if(linha[m]==palavra[p])
    comparaux(n,palavra, linha, m+1,p+1);
    else
    comparaux(n,palavra, linha, m+1,p);
    }
    }
    return n;
    }

    /* A main counter(contprincipal) is declared and inicialized.
    * The variable count as the value of argc.
    * Calls the comparaux function with the current line and argv
    */
    int compara_palavras(char *palavras[],int count,char linha[])
    {
    int cont_principal=0;
    int j;
    for(j=1; j<=(count-2); ++j)
    cont_principal=(comparaux(0, palavras[j], linha,0,0))+cont_principal;
    return cont_principal;
    }


    /* Stores the current line in an array called linha (line)
    * After storing ,calls the function compara_palavras (compare words)
    * with an if clause.If the clause is true, it prints the line
    * stored in the array, and the counter.Goes to the next line.
    */
    int guarda_linha(int protese ,char *grepa[])
    {
    int i=0, j=0;
    char linha[MAX_LINHA];
    char d;
    int c=0;
    if((d=getchar()) != EOF)
    {
    while ((d=getchar()) != '\n' && d!=EOF)
    {
    linha[i]=d;
    ++i;
    }
    linha[i++]='\n';
    if((c=compara_palavras(grepa,protese,linha))!=0)
    {
    printf("LINHA: ");
    for (j=0; linha[j]!='\n'; ++j)
    putchar(linha[j]);
    putchar('\n');
    printf("CONTAGEM: %d\n", c);
    if (d != EOF)
    guarda_linha(protese,grepa);
    }
    else guarda_linha(protese,grepa);
    }
    return 1;
    }

    /* The program must test the command line*/
    main(int argc, char *argv[])
    {
    fflush(stdout);
    if(argc <=2)
    puts("Argumentos inválidos.");
    else
    guarda_linha(argc, argv);
    }

  5. #20
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    People will be more willing to help you if you use code tags.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #21
    Registered User Lost__Soul's Avatar
    Join Date
    Mar 2003
    Posts
    82
    Code:
    #include <stdio.h>
    
    #define MAX_LINHA 128
    
    /* Compares the words received in the command line with those
     * in the array, char by char (the program must detect a word as
     * a subsequence of another word, for example, "um" is a subsequence
     * of "uma"). Returns the counter.
     */
    int comparaux(int n, char palavra[], char linha[], int pos_linha, int pos_palavra)
    {
    	int j=0, p=pos_palavra, m=pos_linha;
    	while(linha[m]!='\n')
    	{
    		if(palavra[p] == '\0')
    		{
    			n=n+1;
    			comparaux(n, palavra, linha, m, 0);
    		}
    		else 
    		{
    			if(linha[m]==palavra[p])
    				comparaux(n,palavra, linha, m+1,p+1);
    			else
    				comparaux(n,palavra, linha, m+1,p);
    		}
    	}
    	return n;
    }
    				
    /* A main counter(contprincipal) is declared and inicialized.
     * The variable count as the value of argc.
     * Calls the comparaux function with the current line and argv
     */
    int compara_palavras(char *palavras[],int count,char linha[])
    {
    	int cont_principal=0;
    	int j;
    	for(j=1; j<=(count-2); ++j)
    		cont_principal=(comparaux(0, palavras[j], linha,0,0))+cont_principal;
    	return cont_principal;
    }
    
    
    /* Stores the current line in an array called linha (line)
     * After storing ,calls the function compara_palavras (compare words)
     * with an if clause.If the clause is true, it prints the line
     * stored in the array, and the counter.Goes to the next line.
     */
    int guarda_linha(int protese ,char *grepa[])
    {
    	int i=0, j=0;
    	char linha[MAX_LINHA];
    	char d;
    	int c=0;
    	if((d=getchar()) != EOF)
    	{
    	while ((d=getchar()) != '\n' && d!=EOF)
    	{
    		linha[i]=d;
    		++i;
    	}
    	linha[i++]='\n';
    	if((c=compara_palavras(grepa,protese,linha))!=0)
    	{
    		printf("LINHA: ");
    		for (j=0; linha[j]!='\n'; ++j)
    			putchar(linha[j]);
    		putchar('\n');
    		printf("CONTAGEM: %d\n", c);
    		if (d != EOF)
    			guarda_linha(protese,grepa);
    	}
    	else guarda_linha(protese,grepa);
    	}
    	return 1;
    }
    
    /* The program must test the command line*/
    main(int argc, char *argv[])
    {
    	fflush(stdout);
    	if(argc <=2)
    		puts("Argumentos inválidos.");
    	else
    		guarda_linha(argc, argv);
    Last edited by Lost__Soul; 03-20-2003 at 09:09 AM.

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read this. You can edit your post (click the EDIT button) to add code tags to your previous post.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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