Skip to main content
Engineering LibreTexts

11.10: Example

  • Page ID
    54313
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This example will scan a string and convert all lower-case letter to upper-case.

    Understand the Problem

    For this problem, the string will be read from the user with a maximum of 80 characters. Any lower case letters encountered will be converted to upper-case. All other characters (digits, symbols, etc.) will be left alone. To determine if a character is lower-case, we can see if it is between “a” and “z”. The final string will be displayed back to the screen. Based on the ASCII table in Appendix A, there is a specific, fixed difference between each upper and lower-case letter. Thus, in order to convert a lower- case character to upper-case, that difference can be subtracted. However, in order to perform the subtraction, each character needs to be converted into an integer (based on its value in the ASCII table). The IACHAR() intrinsic function performs this conversion. After the conversion (subtraction), the integer must be converted back into its corresponding character, which can be accomplished with the ACHAR() intrinsic function. These functions work on a single character/integer, so each character will need to be addressed individually.

    Create the Algorithm

    For this problem, first we will need to prompt for and read the input string. Then any trailing blanks will be removed and the final length can be determined. Based on that length, each character will be accessed and converted if needed.

    ! declare variables
    !     integer -> string1, string2, I, strlen
    ! display initial header
    ! prompt for string
    ! read string
    ! trim any trailing blanks
    ! determine length of string
    ! loop
    !     access each character
    !     if check lower-case ("a" – "z") -> convert to upper-case
    ! display final string
    

    For convenience, the steps are written as program comments.

    Implement the Program

    Based on the algorithm, the below program could be created.

    program caseConverter
    
    ! declare variables
    implicit none
    integer :: i, strlen
    character(80) :: string1
    
    ! ----------
    ! display initial header
        write (*,'(a,/)') "Case Conversion Example"
    
    ! ----------
    ! prompt for string
    ! read string
        write (*,'(a)',advance="no") "Enter String (80 char max): "
        read (*,'(a)') string1
    
    ! ----------
    ! trim any trailing blanks
    ! determine length of string
        strlen = len(trim(string1))
    
    ! loop
        do i = 1, strlen
    
        !     access each character
        !     if check lower-case -> convert to upper-case
        
        if ( string1(i:i) >= "a" .and. string1(i:i) <= "z" ) then
            string1(i:i) = achar( iachar(string1(i:i)) - 32)
        end if
        
        end do
    
    ! ----------
    ! display final string
    
        write (*,'(/,a)') "-------------------------------------"
        write (*,'(a,/,2x,a,/)') "Final String: ", string1
    
    end program caseConverter
    

    The spacing and indentation are not required, but help to make the program more readable.

    Test/Debug the Program

    For this problem, the testing would ensure that the output string is correct for the given input.

    For example, the following output,

    c:\mydir> case
    Case Conversion Example
    
    Enter String (80 char max): Hello World!?
    
    -------------------------------------
    Final String:
      HELLO WORLD!?
    

    Each lower-case letter was converted to upper-case while the upper-case, space, and punctuation were unchanged. The program should be executed with a series of test inputs to verify the correct output.


    This page titled 11.10: Example is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?