1

I am relatively new to Fortran and am attempting to change each character in a string to all uppercase. I am attempting to iterate through the string and for each character utilize its askii value to change it to uppercase and add the character to a new string. the output is close to what I want it to be but does not include spaces.

Program CaesarCode
implicit none
character(len=2000), parameter::phrase="no spaces"

character(len=len_trim(phrase))::uppercase
integer::i
integer::j= len_trim(phrase)
character::c
do i=1, J
if(ichar(phrase(i:i))<123 .and. ichar(phrase(i:i)) >96) then
uppercase(i:i)= achar(ichar(phrase(i:i))-32)
else if(ichar(phrase(i:i))==95) then
uppercase(i:i)= ' '

end if

end do
print *, uppercase
end Program CaesarCode

I plan on adding more logic to deal with mixed case

example output

NOSPACES
  • 1
    You are testing `95` for the space case, but the ascii code of the space character is `32`, not `95`. Moreover you are doing nothing when the input letter is anything else than a lower case or a space (i.e. if there is an upper case or a special character it is dropped: is that what you really want?) – PierU Mar 23 '23 at 08:10
  • I understand, thank you! I plan on adding the logic for when it is an upper case input and a special character next. I wanted to make sure I was able to convert the lowercase into uppercase correctly first. – noah hervey Mar 23 '23 at 08:18
  • However, I still do not understand how the space ends up being skipped. Perhaos just a garbage character. Does it still do this when you set uppercase to an empty string `""` first? – Vladimir F Героям слава Mar 23 '23 at 08:35
  • 2
    There's a lot of magic numbers there (and I haven't got my ASCII table to hand!). Consider replacing ```if(ichar(phrase(i:i))<123 .and. ichar(phrase(i:i)) >96)``` by ```if ( lge( phrase(i:i), 'a' ) .and. lle( phrase(i:i), 'z' ) )``` and the jump of -32 by adding a parameter variable ```atoA = iachar( 'A' ) - iachar( 'a' )```. I think it is simpler just to copy ```phrase``` to ```uppercase``` and then change any characters of the latter that need changing. Also consider putting this in a separate function. – lastchance Mar 23 '23 at 09:13
  • 2
    Have a look at https://stackoverflow.com/questions/63339506/case-insensitive-string-comparisons-in-fortran/63346503. Personally I think it's a mistake to fiddle around with the ASCII codes for characters, let the compiler do that kind of donkey work. – High Performance Mark Mar 23 '23 at 10:08
  • I think I would avoid the encoding issue entirely and do it painfully obviously - one character parameter holding 'A' to 'Z', another with 'a' to 'z', use `scan` to find if/where a character is in the second variable and then use the first to make the conversion. Yes it will be slower, but if this is a significant time in your code you are doing something very strange, and it will be easier to read, IMO. Ahh I see @HighPerformanceMark has just linked to a similar solution. – Ian Bush Mar 23 '23 at 10:10
  • I appreciate all the help, This is my first time coding in Fortran and have realized the syntax is fairly strict and not very intuitive. – noah hervey Mar 24 '23 at 05:36

0 Answers0