此类言论该如何处理?

发布于 2024-12-11 02:49:10 字数 197 浏览 0 评论 0原文

我有一个字符串变量,最多有 80 个字符。 我将变量声明为 CHARACTER STRING*80CHARACTER*80 STRING 如果用户输入的字符少于 80 个,则会在剩余位置附加一些特殊字符。

请问可以告诉我怎么处理吗??

澄清: 我想知道如何用其他字符填充字符串中的剩余空间。

I have String variable which will have maximum 80 characters.
I am declaring a variable as CHARACTER STRING*80 or CHARACTER*80 STRING
If user enters less than 80 characters,then some special characters are getting appended in remaining position.

Could you please let me know how to handle??

CLARIFICATION:
I would like to know how to fill the remaining space in the string with other characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-12-18 02:49:10

使用空字符串预先定义字符串,并用空格填充它。然后使用修剪来仅获取没有尾随空格的字符串。您还应该在声明中使用character(len=80)。

编辑,更新:您可以将字符串中的尾随空格替换为其他字符,例如

do i=len_trim(string)+1,len(string)
  string(i:i) = 'A'
end do

Pre-define your string with an empty one, to fill it with spaces. And afterwards use trim to only get the string without trailing spaces. You should also use character(len=80) in the declaration.

Edit, Update: You can replace the trailing spaces in the string with some other character with something like

do i=len_trim(string)+1,len(string)
  string(i:i) = 'A'
end do
少钕鈤記 2024-12-18 02:49:10

我不明白“如果用户输入的字符少于 80 个,则一些特殊字符将附加在剩余位置。” -- Fortran 方式是在 IO 或赋值时附加空格来填充字符串。您可以通过以下测试程序看到这一点。它还包括 haraldkl 所示的将尾随空格更改为其他字符的方法。问题是很难区分“真正的”尾随空白和填充空白。

module subs

contains

   subroutine write_string (string)

   character (len=10), intent (in) :: string
   integer :: i

   write (*, '(">", A10, "<")' ) string

   do i=1, 10
      write (*, *) i, ichar (string (i:i))
   end do

   return

end subroutine write_string

end module subs


program test_string

use subs

character (len=10) :: string
integer :: i


!  Assignment test

string = "abc"

call  write_string (string)


!  Read test
string = "123456789"

write (*, '("Input string:")' )
read (*, '(A)' ) string

call write_string (string)



!  replacing the trailing blanks:

do i=len_trim (string)+1, 10
   string(i:i) = "+"
end do

call write_string (string)

stop
end program test_string

I don't understand "If user enters less than 80 characters,then some special characters are getting appended in remaining position." -- the Fortran way is to append blanks to fill a string on IO or assignment. You can see that with the following test program. It also includes the method shown by haraldkl to change trailing blanks to some other character. A problem is that it is difficult to distinguish between "real" trailing blanks and ones that are padding.

module subs

contains

   subroutine write_string (string)

   character (len=10), intent (in) :: string
   integer :: i

   write (*, '(">", A10, "<")' ) string

   do i=1, 10
      write (*, *) i, ichar (string (i:i))
   end do

   return

end subroutine write_string

end module subs


program test_string

use subs

character (len=10) :: string
integer :: i


!  Assignment test

string = "abc"

call  write_string (string)


!  Read test
string = "123456789"

write (*, '("Input string:")' )
read (*, '(A)' ) string

call write_string (string)



!  replacing the trailing blanks:

do i=len_trim (string)+1, 10
   string(i:i) = "+"
end do

call write_string (string)

stop
end program test_string
笑看君怀她人 2024-12-18 02:49:10

您可以定义一个由您选择的 80 个字符(例如空格)组成的字符串,然后逐个字符获取输入,从而保持所有元素不变:

[' ', ' ', ' ' ... ' ']

['h', ' ', ' ' ... ' ']

['h', 'e', ' ' ... ' ']

['h', 'e', 'l' ... ' ']

You could define a string of 80 characters of your choice (say, spaces) and then get input character by character, therefore leaving all elements untouched:

[' ', ' ', ' ' ... ' ']

['h', ' ', ' ' ... ' ']

['h', 'e', ' ' ... ' ']

['h', 'e', 'l' ... ' ']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文