Thread: convert ascii lower case to uppercase (c program)

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Post convert ascii lower case to uppercase (c program)

    hey all...There's probably a few bugs. Program works fine on one line of text but won't run through spaces or multiple lines. This program won't convert a C program source file, just ouputs one line and ends when theres a space. Thanks! (very new to programming, about 9 months so far since February of this year), and the if & else if functions might have a few bugs.. thanks for support all! Thanks

    Code:
    #include <stdio.h>
    
    int file_copy(char *oldname, char *newname);
    
    int main( void )
    {
        char source[80], destination[80];
    
        /* Get the source and destination names. */
    
        printf("\nEnter source file: ");
        gets(source);
        printf("\nEnter destination file: ");
        gets(destination);
    
        if(file_copy(source, destination) == 0)
            puts("Copy operation successful");
        else
            fprintf(stderr, "Error during copy operation");
    
        return (0);
    }
    
    int file_copy( char *oldname, char *newname )
    {
        FILE *fold, *fnew;
        int c;
    
        /* Open the source file for reading in binary mode. */ 
    
        if ( ( fold = fopen( oldname, "rb" ) ) == NULL )
            return -1;
        
        /* Open the desintation file for writing in binary mode. */
    
        if ( ( fnew = fopen( newname, "wb" ) ) == NULL )
        {
            fclose ( fold );
            return -1;
        }
    
        while (1)
        {
            c = fgetc ( fold );
            if (!feof(fold))
            {
                // lower case to upper case
                
                if(c >= 'a' && c <= 'z')
                {
                    c = 'A' + c - 'a';
                }    
                
                // upper case stays upper case
                
                else if (c >= 'A' && c <= 'Z')
                {
                    c = c;
                }
                
                // digits 0 through 9 stays the same 
    
                else if (c >= 32 && c <= 127)
                {
                    c = c;
                }
    
                else
                {
                    break;
                }
    
                fputc( c, fnew );
            }
        }
    
    
        fclose ( fnew );
        fclose ( fold );
    
        return 0;
    }
    below: looked at ASCII chart and this isn't running through a long text file (spaces and new lines aren't following as they should)

    Code:
    
                else if (c >= 32 && c <= 127)
                {
                    c = c;
                }
    Last edited by _jamie; 11-23-2018 at 06:46 PM. Reason: missed code tag

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    hey all...There's probably a few bugs.
    No doubt. Probably the biggest bug is the use of the horribly dangerous gets() function, consider fgets() instead.

    Next have you considered using toupper() or tolower() instead of trying to roll your own?

    Be careful, ASCII is not the only type of character encoding and the alpha characters are not guaranteed to be consecutive. However if all you need to worry about is ASCII then do you realize that there is a definite offset between the capital and lower case letters (study the ASCII chart for more information).

    For the last snippet, don't forget about the linefeed and carriage return character and several other whitespace characters that appear before the space character (decimal 32).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    gets is a huge bug: it is inherently vulnerable to buffer overflow.

    Your controlled infinite loop has a structure that looks like it could really be an infinite loop:
    Code:
    while (1)
    {
        c = fgetc ( fold );
        if (!feof(fold))
        {
            // ...
        }
    }
    Suppose feof(fold) returns true. Then you will loop forever. Of course, you might reason that no, before this can ever be the case you will in fact reach the break statement, but that's harder to see and arguably uncertain. It would be better to write:
    Code:
    while ((c = fgetc(fold)) != EOF)
    {
        // ...
    }
    This statement has no net effect:
    Code:
    c = c;
    so when you don't want c to change because it is already in the destination case or not a letter, you should simply structure your code to do nothing for these cases:
    Code:
    while ((c = fgetc(fold)) != EOF)
    {
        // lower case to upper case
        if (c >= 'a' && c <= 'z')
        {
            c = c - 'a' + 'A';
        }
    
        fputc(c, fnew);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Thank's for the help both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upper case= lower case
    By ashley1nonly in forum Tech Board
    Replies: 3
    Last Post: 02-19-2015, 12:52 AM
  2. Upper case= lower case
    By ashley1nonly in forum C Programming
    Replies: 0
    Last Post: 02-18-2015, 10:24 PM
  3. Simple program problem from upper to lower case
    By steals10304 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2009, 02:31 AM
  4. Convert String to Lower Case Crashed
    By woozy in forum C Programming
    Replies: 5
    Last Post: 10-21-2007, 02:50 PM
  5. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM

Tags for this Thread