Hi all,
I have faced some problem ina coding. What I have to do is just to replace all "big" with "small" from the string " The world is big. there are big cities. Bigger city mean bigger population. Towns are getting smaller". My code is

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
main()
{
  FILE *fp,*fp1;
  char c,text[9],big[9]="big",bigger[9]="bigger";
  int sp_cnt=0,i,status1,status2;
 fp = fopen("big.txt","r");
 if(fp==NULL)
  {
   printf("Error in opening file \n");
  }
 else
  {
 printf("valid file \n");
    while(!feof(fp))
     {
       c=getc(fp);
       printf("%c",c);
       if(c==' ')
        {
         sp_cnt++;
        }
     }
    printf("Total no of spaces = %d",sp_cnt);
    fp1=fopen("big.txt","r");
    printf("\n ******************Changed Text****************\n");
    for(i=0;i<sp_cnt+1;i++)
    {
     fscanf(fp1,"%s",text);
     status1 = strcmp(big,text);
     status2 = strncasecmp(bigger,text,6);
     if(status1==0)
       {
           printf(" small");
       }
    else if(status2==0)
       {
           printf(" smaller");
       }
     else
      {
        printf(" %s",text);
      }
    }
   }
fclose(fp);
 }

I am working on unix platform. problem is the first big is not been replaced. Please someone help me out of this.

Recommended Answers

All 3 Replies

if that first loop is trying to count the number of words in the file, it won't work because words can be separated by one or more spaces or tabs. You don't need to count the number of words anyway. just change the second loop to read until fscanf() returns 0.

while( fscanf( fp1,"%s",text) > 0)
{
   // code here
}

also your program needs to close the file handles after finished reading the file.

if that first loop is trying to count the number of words in the file, it won't work because words can be separated by one or more spaces or tabs. You don't need to count the number of words anyway. just change the second loop to read until fscanf() returns 0.

while( fscanf( fp1,"%s",text) > 0)
{
   // code here
}

also your program needs to close the file handles after finished reading the file.

thanks a lot.

Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.