0

Hello
I am writing a file program like that user has to input a string and that string
was written to a file. From that file, that string was read to the another file changing lower case letters to upper case letters and upper case letters to lower case letters.
In my ogram outputs one more character in the second file.
I have been looking for the error,but I can't find. What is the error?

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

int main()
{
FILE *ptr,*ptr1;

char org[30];
char upper[30];
char lower[30];
char change[30];
int i=0,count=0;

if((ptr=fopen("original.txt","w"))!=NULL)
{
printf("\nEnter a string");
gets(org);
fputs(org,ptr);


}
 fclose(ptr);

if((ptr=fopen("original.txt","r"))!=NULL)
{
if((ptr1=fopen("changecase.txt","w"))!=NULL)
{



 while(!feof(ptr))
 {
  org[i]=getc(ptr); printf("%c",org[i]);
  i++;
  count++;

 }
 org[i]='\0';



 for(i=0;i<count;i++)
 {
  upper[i]=toupper(org[i]);
  lower[i]=tolower(org[i]);
 }

 upper[i]='\0';
 lower[i]='\0';



 for(i=0;i<count;i++)
 {
  if(org[i]==upper[i])
     change[i]=lower[i];
     else change[i]=upper[i];
 }


change[i]='\0';

fputs(change,ptr1);

}
}
return 0;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by nucleon
0

This puts one extra character at the end.

while(!feof(ptr))
 {
  org[i]=getc(ptr); printf("%c",org[i]);
  i++;
  count++;
 }
 org[i]='\0';

Instread, try this:

int c, i = 0;
while ((c = getc(ptr)) != EOF)
    org[i++] = c;
count = i;

Also, try using the isupper() and/or islower() functions for the following part.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.