我如何在F90中编写to_upper()或to_lower()函数? [英] How can I write a to_upper() or to_lower() function in F90?

查看:220
本文介绍了我如何在F90中编写to_upper()或to_lower()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个将字符串转换为小写(或者大写)的(Intel)F90函数?我想传递一个字符数组到该函数并让它返回一个字符数组,例如

 程序主
隐含无

字符* 32 :: origStr =你好,世界!
字符* 32 :: newStr

newStr = to_lower(origStr)
写入(*,*)newStr

结束程序主

这样这个程序就输出 hello,world! p>

我已经开始使用在 to_lower()子例程。 org / wiki / String_case#Fortranrel =noreferrer> RosettaCode ,但我无法弄清楚如何将它编写为函数。

预先感谢!

PS - 奖励积分,如果你可以用一串不固定的长度来做!

解决方案

作为此代码的原始作者,我很高兴它有一些帮助。我曾经想知道为什么这些函数不是内置在Fortran中的。我的猜测是,他们只能用于一组相当有限的字母,即英文中使用的字母。如果几乎任何其他欧洲语言都有文本,则会有带重音符的字符,然后将它们转换为大写或小写更难。例如法语中的电子坟墓变成大写字母通常显示为简单的E(重音重音会丢失),但在电子尖锐中则不会。 Fortran的设计人员一直试图提供适合各种语言的设施,并且以多种语言进行大小写转换并非易事。至少这是我的猜测,为什么你必须自己做。

How does one write a (Intel) F90 function that converts a string into lowercase (or, alternatively, uppercase)? I want to pass a character array to the function and have it return a character array, e.g.

program main
    implicit none

    character*32 :: origStr = "Hello, World!"
    character*32 :: newStr

    newStr = to_lower(origStr)
    write (*,*) newStr

end program main

such that this program outputs hello, world!.

I've been starting with the to_lower() subroutine found at RosettaCode, but I can't figure out how to write it as a function.

Thanks in advance!

PS -- Bonus points if you can do it with a string of unfixed length!

解决方案

As the original author of this code, I'm pleased that it's of some help. I used to wonder why these functions were not built in to Fortran. My guess is that they only work for a rather restricted set of letters, i.e. the ones used in English. If you have text in almost any other European language you will have characters with accents, and then converting them to upper or lower case is much harder. For example e-grave in French turned into upper case is usually shown as just plain E (the grave accent gets lost), but in e-acute it does not. The designers of Fortran have always tried to provide facilities which suit a wide range of languages, and doing upper/lower case conversion in a multi-language way is not at all easy. At least that's my guess as to why you have to do it yourself.

这篇关于我如何在F90中编写to_upper()或to_lower()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆