Iam3R 24 Junior Poster

And another extremely lightweight unit testing framework is CuTest

its really good .
but i dont understand are these the rules that are followed in the industry.

Iam3R 24 Junior Poster

I hope what he wants he is just " find the middle element when you have given three elements"

Ex: 1,3,6 the middle element is 3 ( this we cant get just by dividing)
5,6,7 the the middle element is 6.

if this is the case :

int find_mid(int n1, int n2, int n3)
{
  int mid;
  if( n1 > n2 )   {
          if( n3 > n2)
                {
                        if( n3 < n1 )
                                mid = n3;
                        else
                                mid = n1;
                }
         else {
                mid = n2;
        }
 }
 else {
        if( n2 > n3 )
         {
                if ( n1 > n3)
                  mid = n1;
                else
                mid = n3;
         }
        else{
                mid = n2;
        }
}
return mid ;
}

the driver:

int main()
{
        printf( "%d  ", find_mid(3,4,7) );
        printf( "%d  ", find_mid(5,2,8) );
        printf( "%d  ", find_mid(2,9,4) );
        printf( "%d  ", find_mid(7,5,9) );
        printf( "%d  ", find_mid(7,4,6) );
        printf( "%d  ", find_mid(1,3,6) );
        printf( "%d  ", find_mid(5,6,7) );
return 0;
}
Iam3R 24 Junior Poster

i hope this is what you want ..

i just edited , but some unneccessary statements/ variable are present that will not harm our prgram.

#include<stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000
#define SIZE 100
void search_string(char *file, char *pat);

int main()
{
      int counter = 0;
      int i=0;
     char ch;
     const char filename[] = "in.txt";
     char pat[50];
    FILE *file = fopen(filename, "r");
    if ( file != NULL )
    {
     char line [ MAX ];
     while(isspace(ch=fgetc(file))) ;
     pat[i++] = ch;
     while(!(isspace(ch=fgetc(file))))
     pat[i++] = ch;
     pat[i] = '\0';
     rewind(file);
     while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
     {
            ++counter;
            printf( "%d  ", counter);
            fputs ( line, stdout ); /* write the line */
            search_string(line,pat);
   }
      printf( "\n\n");
      //search_string(line);
      fclose ( file );
   }
   else
   {
      perror ( filename );
   }

   char wait;
   scanf( "%c", &wait );
   return(0);
}
void search_string(char *file, char *pat)
{
        char *ptr, *strptr, word[SIZE] = {" "};
        int i = 0, j = 0;
        int spc =0;
           /*
        while (*ptr != '\0')
        {
                if (isspace(*ptr) && spc == 0)
                                 {
                                    spc  = 0;
                                         }
                if(spc != 0 && isspace(*ptr))
                                                break;
                if(isalnum(*ptr))
                                {
                                    spc =1;
                                    word[i] = *ptr ; // file[i];
        / / here file[i] is blank , is a problem do you really want that
                                   i++;
                        }
                    ptr++;
                 }
              word[i]='\0';
             */

        strptr = file;
        while ((strptr = strstr(strptr, pat)) != NULL)
        {
                strptr++;
                j++;
        }
        printf("%s occured %d times in the string\n", pat, j);
}
Grn Xtrm commented: Thanks for the help. +1
Iam3R 24 Junior Poster

But, is there any proposed solution to what I'm looking for?

you can also use function pointers to do same thing.

Iam3R 24 Junior Poster

I got my function to work somewhat but it only prints the number of occurrences of the first word of the last line in the last line only.

because you are calling the function search_string(line); after the statement

while ( fgets ( line, sizeof line, file ) != NULL )
{
        :
        :
}

by the time it comes out of while line is having last line of the file.


I just want to get the program to search all lines for the first word of the txt file.

probably you want to search for the first word(Secret) in all line
you can modify so easily.

i have just edited your code, i dont completly understand your requirement , but this works fine for all the lines.
you can modify as per your Req:

Here is the revised code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000
#define SIZE 100
void search_string(char *file);

int main()
{
    int counter = 0;
    const char filename[] = "in.txt";        
    FILE *file = fopen(filename, "r");    
    if ( file != NULL )
    {
     char line [ MAX ]; 

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
            ++counter;
            printf( "%d  ", counter);
            fputs ( line, stdout ); /* write the line */
            search_string(line);   
   }
      printf( "\n\n");
      //search_string(line); 
      fclose ( file );
   }
   else
   {
      perror ( filename ); 
   }
   
   char wait;
   scanf( "%c", &wait );
   return(0);
} …