Thread: Problem reading file and converting to upper case

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    25

    Problem reading file and converting to upper case

    vlez.txt
    ova e prvata greshna rechenica. i
    narednava rechenica, treba da ja
    promente! Tretava rechenica e pravilno
    napishana. a ovaa?

    izlez.txt
    Ova e prvata greshna rechenica. I
    narednava rechenica, treba da ja
    promente! Tretava rechenica e pravilno
    napishana. А ovaa?

    If the letter after . ! is lower - to upper, between
    . and first letter is only one space

    I wrote this code, but prints letters at the end of text, does not change lower to upper !


    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
    
    
    {
        FILE *vlez;
        FILE *izlez;
        char c;
        char k;
        char tekst[500];
        int i=0,j;
        int b=0;
    
    
        if((vlez=fopen("vlezna.txt", "r"))==NULL)
        {
            printf("Ne moze da se otfori datotekata ! \n");
        }
        else if((izlez=fopen("izlezna.txt", "w"))==NULL)
        {
            printf("Ne moze da se kreira datoteka ! \n");
        }
    
    
        else{
    while((c=fgetc(vlez))!=EOF)
    
    
    {
             if(b==1)
             {
                 
                 if(islower(tekst[i]))
                 {
                     toupper(tekst[i]);
                 }
                 fprintf(izlez, "%c", tekst[i]);
             }
                if(isalpha(c))
            {
                tekst[i]=c;
                fprintf(izlez, "%c", tekst[i]);
                i++;
            }
            else if(isspace(c))
            {
                tekst[i]=c;
                fprintf(izlez, "%c", tekst[i]);
                i++;
            }
            else if(!isspace(c)&&!isalnum(c))
            {
                tekst[i]=c;
                fprintf(izlez, "%c",tekst[i] );
                fprintf(izlez, " ");
                b=1;
                i+3;
            }
            }
            for(j=0; j<i; j++)
            {
               if (!isspace(tekst[j])&&!isalnum(tekst[j]))
               {
                   k=tekst[j+2];
                   if(islower(k))
                   {
                       k=toupper(k);
                   }
               }
            }
        }
        fclose(vlez);
        fclose(izlez);
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Step 1: Neaten up the formatting of your code. You have decent indentation, but things get a kind of wonky in a few spots. Completely consistent formatting/indentation is key in helping your eye see the flow of code properly.



    Step 2: Check your compiler warnings:

    Code:
    /*
    main.c||In function 'main':|
    main.c|37|warning: statement with no effect|
    main.c|59|warning: statement with no effect|
    ||=== Build finished: 0 errors, 2 warnings ===|
    */
    "toupper()" returns the argument (upper-cased, if the argument was valid), so you need to store this result somewhere.

    The second one just does nothing: "i+3;" Did you mean to do "i = i + 3"?



    Step 3: You need to neaten up your file handling code. You shouldn't close the file unless it had been successfully opened. And if the second file failed to open, you should only close the first.



    Step 4: Use descriptive variable names. For instance, what does "b" represent? Use variable names that help describe its purpose, so the code is easier to read and understand.



    Step 5: I strongly suggest you plan your logic out on paper before attempting the code. This has the appearance of code that was typed out off the top of your head. A flow chart might be helpful in the design of the logic necessary to complete this task.

    As it stands, I have no desire trying to untangle this code to find out why it isn't working. I'd suggest either (1) using a debugger to help track down the issue(s), or (2) start over. If you start over, try writing a flow chart (or even pseudo-code) to plan the logic.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Code:
    else if(!isspace(c)&&!isalnum(c))        
    {
           tekst[i]=c;
           fprintf(izlez, "%c",tekst[i] );
           fprintf(izlez, " ");
           b=1;
           i+3;
    }
    A lot of this code looks to be very specific to the file in question, but the problem could be that the variable i, or b isn't what you were planning it to be.

    For instance, here you say "i+3", but that value isn't being assigned anywhere. Also, did you mean to reset "b" to a non 1 value at some point? As it stands, toupper() is being called for every lowercase character after b is set to 1.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char c;
    ...
    while((c=fgetc(vlez))!=EOF)
    c should be declared as int and not char, this is especially important because of the test being made against EOF..
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program problem from upper to lower case
    By steals10304 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2009, 02:31 AM
  2. help...Lower and Upper case problem only
    By j4k50n in forum C Programming
    Replies: 9
    Last Post: 04-19-2007, 12:39 AM
  3. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM
  4. converting a string to upper case
    By cxs00u in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2002, 10:40 AM
  5. upper case...
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2002, 01:32 AM