Skip to content Skip to sidebar Skip to footer

C++ Read Char in String in Array of Strings

Assortment of Strings in C

Last updated on July 27, 2020


What is an Assortment of Strings? #

A string is a 1-D array of characters, so an assortment of strings is a 2-D assortment of characters. Merely like we tin can create a ii-D assortment of int, float etc; we tin can also create a 2-D assortment of grapheme or array of strings. Here is how we can declare a 2-D array of characters.

                                            char                      ch_arr                      [                      3                      ][                      ten                      ]                      =                      {                      {                      'southward'                      ,                      'p'                      ,                      'i'                      ,                      '1000'                      ,                      'eastward'                      ,                      '\0'                      },                      {                      't'                      ,                      'o'                      ,                      'm'                      ,                      '\0'                      },                      {                      'j'                      ,                      'e'                      ,                      'r'                      ,                      'r'                      ,                      'y'                      ,                      '\0'                      }                      };                    

Information technology is important to end each 1-D array past the zilch character, otherwise, it will be simply an array of characters. We can't use them as strings.

Declaring an array of strings this way is rather tedious, that'south why C provides an culling syntax to achieve the same thing. This higher up initialization is equivalent to:

                                            char                      ch_arr                      [                      3                      ][                      ten                      ]                      =                      {                      "spike"                      ,                      "tom"                      ,                      "jerry"                      };                    

The showtime subscript of the array i.due east iii denotes the number of strings in the array and the second subscript denotes the maximum length of the cord. Recall the that in C, each character occupies i byte of information, so when the compiler sees the above statement it allocates 30 bytes (three*x) of retentivity.

We already know that the name of an assortment is a pointer to the 0th element of the array. Can you guess the type of ch_arr?

The ch_arr is a arrow to an array of ten characters or int(*)[ten].

Therefore, if ch_arr points to address 1000 then ch_arr + 1 will signal to accost 1010.

From this, we can conclude that:

ch_arr + 0 points to the 0th cord or 0th one-D array.
ch_arr + one points to the 1st cord or 1st i-D array.
ch_arr + two points to the 2nd string or 2nd one-D array.

In general, ch_arr + i points to the ith string or ith 1-D array.

We know that when we dereference a arrow to an array, we get the base of operations accost of the array. Then, on dereferencing ch_arr + i we get the base address of the 0th 1-D array.

From this we tin can conclude that:

*(ch_arr + 0) + 0 points to the 0th character of 0th i-D array (i.e southward)
*(ch_arr + 0) + i points to the 1st graphic symbol of 0th i-D array (i.due east p)
*(ch_arr + 1) + ii points to the 2nd graphic symbol of 1st 1-D array (i.e m)

In general, we tin say that: *(ch_arr + i) + j points to the jth character of ith 1-D array.

Note that the base type of *(ch_arr + i) + j is a pointer to char or (char*), while the base type of ch_arr + i is assortment of x characters or int(*)[10].

To get the element at jth position of ith i-D assortment simply dereference the whole expression*(ch_arr + i) + j.

We have learned in chapter Pointers and two-D arrays that in a 2-D array the pointer notation is equivalent to subscript note. So the above expression can be written equally follows:

The following plan demonstrates how to print an array of strings.

                      1  ii  iii  iv  5  6  7  8  9 x 11 12 13 14 15 16 17 18 nineteen 20 21 22
                                            #include                      <stdio.h>                                            int                      chief                      ()                      {                      int                      i                      ;                      char                      ch_arr                      [                      3                      ][                      x                      ]                      =                      {                      "spike"                      ,                      "tom"                      ,                      "jerry"                      };                      printf                      (                      "1st way                                            \northward\n                      "                      );                      for                      (                      i                      =                      0                      ;                      i                      <                      3                      ;                      i                      ++                      )                      {                      printf                      (                      "cord = %s                                            \t                                              accost = %u                      \north                      "                      ,                      ch_arr                      +                      i                      ,                      ch_arr                      +                      i                      );                      }                      // signal to operating organization program ran fine                      render                      0                      ;                      }                    

Expected Output:

                      string = spike address = 2686736 string = tom address = 2686746 string = jerry address = 2686756                    

Some invalid operation on an Array of string #

                                            char                      ch_arr                      [                      3                      ][                      10                      ]                      =                      {                      {                      's'                      ,                      'p'                      ,                      'i'                      ,                      '1000'                      ,                      'e'                      ,                      '\0'                      },                      {                      't'                      ,                      'o'                      ,                      'one thousand'                      ,                      '\0'                      },                      {                      'j'                      ,                      'e'                      ,                      'r'                      ,                      'r'                      ,                      'y'                      ,                      '\0'                      }                      };                    

It allocates 30 bytes of memory. The compiler will practise the same thing even if we don't initialize the elements of the array at the time of declaration.

We already know that the proper name of an assortment is a constant arrow then the post-obit operations are invalid.

                                            ch_arr                      [                      0                      ]                      =                      "tyke"                      ;                      // invalid                      ch_arr                      [                      one                      ]                      =                      "dragon"                      ;                      // invalid                    

Hither we are trying to assign a cord literal (a arrow) to a constant arrow which is obviously not possible.

To assign a new string to ch_arr apply the following methods.

                                            strcpy                      (                      ch_arr                      [                      0                      ],                      "type"                      );                      // valid                      scanf                      (                      ch_arr                      [                      0                      ],                      "type"                      );                      // valid                    

Let's conclude this chapter by creating some other simple programme.

This plan asks the user to enter a username. If the username entered is ane of the names in the master list then the user is allowed to calculate the factorial of a number. Otherwise, an fault message is displayed.

                      1  2  3  4  five  6  7  8  ix 10 11 12 13 fourteen 15 sixteen 17 xviii 19 xx 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 fifty 51 52 53 54 55 56 57
                                            #include                      <stdio.h>                                            #include                      <string.h>                                            int                      factorial                      (                      int                      );                      int                      master                      ()                      {                      int                      i                      ,                      found                      =                      0                      ,                      n                      ;                      char                      master_list                      [                      5                      ][                      20                      ]                      =                      {                      "admin"                      ,                      "tom"                      ,                      "bob"                      ,                      "tim"                      ,                      "jim"                      },                      name                      [                      10                      ];                      printf                      (                      "Enter username: "                      );                      gets                      (                      proper name                      );                      for                      (                      i                      =                      0                      ;                      i                      <                      5                      ;                      i                      ++                      )                      {                      if                      (                      strcmp                      (                      proper noun                      ,                      master_list                      [                      i                      ])                      ==                      0                      )                      {                      establish                      =                      1                      ;                      suspension                      ;                      }                      }                      if                      (                      found                      ==                      1                      )                      {                      printf                      (                      "                      \northward                      Welcome %south !                      \n                      "                      ,                      name                      );                      printf                      (                      "                      \north                      Enter a number to summate the factorial: "                      );                      scanf                      (                      "%d"                      ,                      &                      northward                      );                      printf                      (                      "Factorial of %d is %d"                      ,                      due north                      ,                      factorial                      (                      due north                      ));                      }                      else                      {                      printf                      (                      "Fault: You are not allowed to run this programme."                      ,                      name                      );                      }                      // point to operating system program ran fine                      return                      0                      ;                      }                      int                      factorial                      (                      int                      n                      )                      {                      if                      (                      n                      ==                      0                      )                      {                      return                      one                      ;                      }                      else                      {                      return                      n                      *                      factorial                      (                      n                      -                      1                      );                      }                      }                    

Expected Output: 1st run:

                      Enter username: admin  Welcome admin !  Enter a number to summate the factorial: four Factorial of iv is 24                    

second run:

                      Enter username: jack Error: You are not allowed to run this program.                    

How it works:

The plan asks the user to enter a name. Subsequently the name is entered it compares the entered name with the names in the master_list array using strcmp() role. If match is found and then strcmp() returns 0 and the if condition strcmp(name, master_list[i]) == 0 condition becomes true. The variable found is assigned a value of 1, which means that the user is allowed to access the program. The programme asks the user to enter a number and displays the factorial of a number.

If the name entered is not one of the names in the master_list array then the plan exits by displaying an error message.



hopkinsexames.blogspot.com

Source: https://overiq.com/c-programming-101/array-of-strings-in-c/

Post a Comment for "C++ Read Char in String in Array of Strings"