{"id":2873,"date":"2024-05-21T06:05:19","date_gmt":"2024-05-21T06:05:19","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=2873"},"modified":"2024-05-21T06:05:20","modified_gmt":"2024-05-21T06:05:20","slug":"characters","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/05\/21\/characters\/","title":{"rendered":"Characters"},"content":{"rendered":"\n<p>The Fortran language can treat characters as single character or contiguous strings.<\/p>\n\n\n\n<p>Characters could be any symbol taken from the basic character set, i.e., from the letters, the decimal digits, the underscore, and 21 special characters.<\/p>\n\n\n\n<p>A character constant is a fixed valued character string.<\/p>\n\n\n\n<p>The intrinsic data type&nbsp;<strong>character<\/strong>&nbsp;stores characters and strings. The length of the string can be specified by&nbsp;<strong>len<\/strong>&nbsp;specifier. If no length is specified, it is 1. You can refer individual characters within a string referring by position; the left most character is at position 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Character Declaration<\/h2>\n\n\n\n<p>Declaring a character type data is same as other variables \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type-specifier :: variable_name\n<\/code><\/pre>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>character :: reply, sex<\/code><\/pre>\n\n\n\n<p>you can assign a value like,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>reply = \u2018N\u2019 \nsex = \u2018F\u2019<\/code><\/pre>\n\n\n\n<p>The following example demonstrates declaration and use of character data type \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program hello\nimplicit none\n\n   character(len = 15) :: surname, firstname \n   character(len = 6) :: title \n   character(len = 25)::greetings\n   \n   title = 'Mr. ' \n   firstname = 'Rowan ' \n   surname = 'Atkinson'\n   greetings = 'A big hello from Mr. Bean'\n   \n   print *, 'Here is ', title, firstname, surname\n   print *, greetings\n   \nend program hello<\/code><\/pre>\n\n\n\n<p>When you compile and execute the above program it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Here is Mr. Rowan Atkinson       \nA big hello from Mr. Bean\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Concatenation of Characters<\/h2>\n\n\n\n<p>The concatenation operator \/\/, concatenates characters.<\/p>\n\n\n\n<p>The following example demonstrates this \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program hello\nimplicit none\n\n   character(len = 15) :: surname, firstname \n   character(len = 6) :: title \n   character(len = 40):: name\n   character(len = 25)::greetings\n   \n   title = 'Mr. ' \n   firstname = 'Rowan ' \n   surname = 'Atkinson'\n   \n   name = title\/\/firstname\/\/surname\n   greetings = 'A big hello from Mr. Bean'\n   \n   print *, 'Here is ', name\n   print *, greetings\n   \nend program hello<\/code><\/pre>\n\n\n\n<p>When you compile and execute the above program it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Here is Mr.Rowan Atkinson       \nA big hello from Mr.Bean\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Some Character Functions<\/h2>\n\n\n\n<p>The following table shows some commonly used character functions along with the description \u2212<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Sr.No<\/th><th>Function &amp; Description<\/th><\/tr><tr><td>1<\/td><td><strong>len(string)<\/strong>It returns the length of a character string<\/td><\/tr><tr><td>2<\/td><td><strong>index(string,sustring)<\/strong>It \ufb01nds the location of a substring in another string, returns 0 if not found.<\/td><\/tr><tr><td>3<\/td><td><strong>achar(int)<\/strong>It converts an integer into a character<\/td><\/tr><tr><td>4<\/td><td><strong>iachar(c)<\/strong>It converts a character into an integer<\/td><\/tr><tr><td>5<\/td><td><strong>trim(string)<\/strong>It returns the string with the trailing blanks removed.<\/td><\/tr><tr><td>6<\/td><td><strong>scan(string, chars)<\/strong>It searches the &#8220;string&#8221; from left to right (unless back=.true.) for the first occurrence of any character contained in &#8220;chars&#8221;. It returns an integer giving the position of that character, or zero if none of the characters in &#8220;chars&#8221; have been found.<\/td><\/tr><tr><td>7<\/td><td><strong>verify(string, chars)<\/strong>It scans the &#8220;string&#8221; from left to right (unless back=.true.) for the first occurrence of any character not contained in &#8220;chars&#8221;. It returns an integer giving the position of that character, or zero if only the characters in &#8220;chars&#8221; have been found<\/td><\/tr><tr><td>8<\/td><td><strong>adjustl(string)<\/strong>It left justifies characters contained in the &#8220;string&#8221;<\/td><\/tr><tr><td>9<\/td><td><strong>adjustr(string)<\/strong>It right justifies characters contained in the &#8220;string&#8221;<\/td><\/tr><tr><td>10<\/td><td><strong>len_trim(string)<\/strong>It returns an integer equal to the length of &#8220;string&#8221; (len(string)) minus the number of trailing blanks<\/td><\/tr><tr><td>11<\/td><td><strong>repeat(string,ncopy)<\/strong>It returns a string with length equal to &#8220;ncopy&#8221; times the length of &#8220;string&#8221;, and containing &#8220;ncopy&#8221; concatenated copies of &#8220;string&#8221;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1<\/h3>\n\n\n\n<p>This example shows the use of the&nbsp;<strong>index<\/strong>&nbsp;function \u2212<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/ckq0yW\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program testingChars\nimplicit none\n\n   character (80) :: text \n   integer :: i \n   \n   text = 'The intrinsic data type character stores characters and   strings.'\n   i=index(text,'character') \n   \n   if (i \/= 0) then\n      print *, ' The word character found at position ',i \n      print *, ' in text: ', text \n   end if\n   \nend program testingChars<\/code><\/pre>\n\n\n\n<p>When you compile and execute the above program it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The word character found at position 25\nin text : The intrinsic data type character stores characters and strings.  \n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2<\/h3>\n\n\n\n<p>This example demonstrates the use of the&nbsp;<strong>trim<\/strong>&nbsp;function \u2212<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/0qWarl\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program hello\nimplicit none\n\n   character(len = 15) :: surname, firstname \n   character(len = 6) :: title \n   character(len = 25)::greetings\n   \n   title = 'Mr.' \n   firstname = 'Rowan' \n   surname = 'Atkinson'\n   \n   print *, 'Here is', title, firstname, surname\n   print *, 'Here is', trim(title),' ',trim(firstname),' ', trim(surname)\n   \nend program hello<\/code><\/pre>\n\n\n\n<p>When you compile and execute the above program it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Here isMr.   Rowan          Atkinson       \n Here isMr. Rowan Atkinson\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3<\/h3>\n\n\n\n<p>This example demonstrates the use of&nbsp;<strong>achar<\/strong>&nbsp;function \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program testingChars\nimplicit none\n\n   character:: ch\n   integer:: i\n   \n   do i = 65, 90\n      ch = achar(i)\n      print*, i, ' ', ch\n   end do\n   \nend program testingChars<\/code><\/pre>\n\n\n\n<p>When you compile and execute the above program it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>65  A\n66  B\n67  C\n68  D\n69  E\n70  F\n71  G\n72  H\n73  I\n74  J\n75  K\n76  L\n77  M\n78  N\n79  O\n80  P\n81  Q\n82  R\n83  S\n84  T\n85  U\n86  V\n87  W\n88  X\n89  Y\n90  Z\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Checking Lexical Order of Characters<\/h2>\n\n\n\n<p>The following functions determine the lexical sequence of characters \u2212<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Sr.No<\/th><th>Function &amp; Description<\/th><\/tr><tr><td>1<\/td><td><strong>lle(char, char)<\/strong>Compares whether the first character is lexically less than or equal to the second<\/td><\/tr><tr><td>2<\/td><td><strong>lge(char, char)<\/strong>Compares whether the first character is lexically greater than or equal to the second<\/td><\/tr><tr><td>3<\/td><td><strong>lgt(char, char)<\/strong>Compares whether the first character is lexically greater than the second<\/td><\/tr><tr><td>4<\/td><td><strong>llt(char, char)<\/strong>Compares whether the first character is lexically less than the second<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example 4<\/strong><\/p>\n\n\n\n<p>The following function demonstrates the use \u2212<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/n9IbUu\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program testingChars\nimplicit none\n\n   character:: a, b, c\n   a = 'A'\n   b = 'a'\n   c = 'B'\n   \n   if(lgt(a,b)) then\n      print *, 'A is lexically greater than a'\n   else\n      print *, 'a is lexically greater than A'\n   end if\n   \n   if(lgt(a,c)) then\n      print *, 'A is lexically greater than B'\n   else\n      print *, 'B is lexically greater than A'\n   end if  \n   \n   if(llt(a,b)) then\n      print *, 'A is lexically less than a'\n   end if\n   \n   if(llt(a,c)) then\n      print *, 'A is lexically less than B'\n   end if\n   \nend program testingChars<\/code><\/pre>\n\n\n\n<p>When you compile and execute the above program it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a is lexically greater than A\nB is lexically greater than A\nA is lexically less than a\nA is lexically less than B\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Fortran language can treat characters as single character or contiguous strings. Characters could be any symbol taken from the basic character set, i.e., from the letters, the decimal digits, the underscore, and 21 special characters. A character constant is a fixed valued character string. The intrinsic data type&nbsp;character&nbsp;stores characters and strings. The length of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[222],"tags":[],"class_list":["post-2873","post","type-post","status-publish","format-standard","hentry","category-02-fortran"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=2873"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2873\/revisions"}],"predecessor-version":[{"id":2874,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2873\/revisions\/2874"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=2873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=2873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=2873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}