Skip to content Skip to sidebar Skip to footer

Read File to Char Array in C

In this C Programming tutorial, nosotros will talk over 2D character arrays in detail and will discuss how to declare, initialize and use 2nd grapheme arrays or String arrays.

ane. What is 2d character array in C?

Nosotros have successfully learned the basic concepts and different library functions that C Programming offers. Another interesting concept is the use of 2D grapheme arrays. In the previous tutorial, we already saw that string is zilch merely an array of characters that ends with a '\0'.

second character arrays are very similar to 2D integer arrays. We store the elements and perform other operations in a similar manner. A 2D grapheme array is more than like a String array . It allows us to shop multiple strings nether the same proper name.


2. How to Annunciation and Initialization a 2D character array?

A 2nd grapheme array is declared in the following manner:

          char name[5][x];        

The order of the subscripts is important during declaration. The offset subscript [5] represents the number of Strings that we desire our assortment to comprise and the second subscript [x] represents the length of each Cord. This is static memory allocation. We are giving 5*10=50 memory locations for the array elements to be stored in the array.

Initialization of the grapheme array occurs in this manner:

char name[v][ten]={                    "tree",                    "bowl",                    "chapeau",                    "mice",                    "toon"                  };        

Let's see the diagram below to understand how the elements are stored in the memory location:

2D char array

The areas marked in green show the memory locations that are reserved for the array but are not used by the cord. Each grapheme occupies one byte of storage from the retentivity.


three. How to take 2D array Data input from user?

In order to accept string information input from the user nosotros demand to follow the post-obit syntax:

for(i=0 ;i<5 ;i++ ) scanf("%s",&name[i][0]);        

Here we see that the second subscript remains [0]. This is because information technology shows the length of the string and before entering any string the length of the string is 0.


4. Printing the assortment elements

The manner a 2D graphic symbol assortment is printed is not the same as a 2nd integer array. This is because we meet that all the spaces in the array are not occupied past the string entered by the user.

If we display information technology in the aforementioned way equally a 2D integer array we will get unnecessary garbage values in unoccupied spaces. Hither is how we tin can display all the cord elements:

for(i=0 ;i<5 ;i++) printf("%due south\north",proper name[i]);        

This format will impress only the string contained in the alphabetize numbers specified and eliminate any garbage values afterward '\0'. All the string library functions that we take come beyond in the previous tutorials tin exist used for the operations on strings contained in the string array. Each string can be referred to in this form:

          name[i][0];        

where [i] is the index number of the string that needs to be accessed past library functions.


five. Program to search for a string in the string array

Let'south implement a program to search for a string(a char assortment) entered by the user in a 2D char array or a string array(also entered past the user):

#include <stdio.h> #include <string.h> int principal() {   char name[five][10],       item[ten]; // declaring the string array and the grapheme                 // array that will comprise the string to exist matched   int i, x, f = 0;    /*taking 5 cord inputs*/   printf("Enter 5 strings:\north");   for (i = 0; i < v; i++)     scanf("%southward", &proper name[i][0]);    /*inbound the particular to be institute in the string array*/   printf("Enter the cord to be searched:\n");   scanf("%s", &item);    /*procedure for finding the item in the string array*/   for (i = 0; i < 5; i++) {     10 = strcmp(&name[i][0],                item); // compares the string in the array with the item and if                       // match is institute returns 0 and stores it in variable x     if (x == 0)       f = i;   }    /*if match is not found*/   if (f == 0)     printf("the detail does not match any name in the list");   /*If the match is found*/   else     printf("Item Found. Item in the assortment exists at index - %d", f);   render 0; }        
                      Output:-                    Enter five strings: tree bowl hat mice toon Enter the string to be searched: mice Item Found. Item in the array exists at index - 3        

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial serial.

Also for the instance C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in noesis ever pays the best involvement. I hope you like the tutorial. Do come up back for more considering learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

moorelecladmands.blogspot.com

Source: https://www.codingeek.com/tutorials/c-programming/2d-character-array-string-array-declaration-and-initialization/

Post a Comment for "Read File to Char Array in C"