Strings in C

Mannan Ul Haq
0

Character Arrays or C-Strings:

In C programming, there are no built-in string data types like in other high-level languages. Instead, character arrays, often referred to as C-strings, are used to represent and manipulate strings of characters. A C-string is a sequence of characters stored in a one-dimensional character array and is terminated by a null character '\0' to indicate the end of the string. This null character acts as a sentinel value and is essential for functions that work with C-strings to know where the string ends. Because C does not have a native string data type, character arrays provide a simple and efficient way to handle textual data. C-strings are widely used in C programming for various purposes, such as storing text, reading input, and performing string manipulations.


Difference between 'A' and "A":

  • The first one is character A; and the second one is C-string A.
  • Because C-strings are null terminated, "A" represents two characters: 'A' and '\0'. So it takes two memory cells.

Declaration and Initialization:

// Declaration
char arrayName[arraySize];


   Following are the methods to initialize the character array:


1. We can also initialize a character array at the time of declaration, either by providing a string literal or by assigning individual characters.


char name[5] = { 'J', 'o', 'h', 'n', '\0' };


OR

char name[5] = "John";


The size of the array is automatically determined based on the length of the string literal, including the null character.


OR 

char name[] = "John";


 2. Input by user:

You can also take input from the user and store it in a character array using scanf. For example:


char name[10];
printf("Enter your name: ");
scanf_s("%9s", name, 10); // Limit the input to 9 characters to leave space for the null terminator

In this code snippet, the user is prompted to enter their name, and the input is stored in the character array name. The %9s format specifier in scanf ensures that the input is limited to nine characters, leaving space for the null terminator.


Printing Array Elements:

To print a C-string stored in a character array, you can use the printf function with the %s format specifier:

char myArray[] = "Hello World!";
printf("The character array: %s\n", myArray);


The %s format specifier in printf is used to display C-strings. It automatically prints the characters from the array until it encounters the null character '\0', which marks the end of the string.


Usage of "fgets" Function in Character Arrays:

C also provides the fgets function to read input as a line of text, including spaces. fgets takes the input stream, character array, and the maximum number of characters to read as its arguments:


#include <stdio.h>

int main()
{
    #define MAX_LENGTH 100
    char input[MAX_LENGTH];

    printf("Enter a line of text: ");
    fgets(input, MAX_LENGTH, stdin);

    printf("You entered: %s", input);

    return 0;
}


Find length of String:

To find the length of a string using the `strlen` function, you need to include the `<string.h>` header file, which provides the declaration for the `strlen` function. The `strlen` function takes a null-terminated character array (string) as its argument and returns the number of characters in the string, excluding the null character ('\0').


Here's an example that demonstrates how to use `strlen` to find the length of a string:


#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "Hello World!";
    int length = strlen(str) + 1; // We are adding 1 because the 'strlen' function excludes the null character

    printf("Length of the string: %d\n", length); // Outputs 13

    return 0;
}


2D-Character Array:

A 2D character array, also known as a matrix or grid of characters, is an array where each element is itself an array of characters. It can be used to store and manipulate strings or characters in a two-dimensional structure.


Initialization of a 2D character array:

1. During declaration of array:


char list[3][7] = { "Mango", "Apple", "Banana" };

 

2. Using loops:


#define rows 3
#define columns 7
char arr[rows][columns];

for (int i = 0; i < rows; i++)
{
    printf("Enter string for row %d: ", i + 1);
    scanf("%6s", arr[i]);
}


Printing Elements of Array:

for (int i = 0; i < rows; i++)
    printf("%s\n", arr[i]);



Arrays in C++ provide a powerful tool for storing and manipulating collections of elements. They are versatile and widely used in various programming tasks.


Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !