C Strings

Concept about 'string.h' in C.

* String manipulation in C required the "string.h" header file. This header file provides prototypes for functions like 'strcpy', 'strcat', 'strcmp', 'strlen', and others.

But, before directly going to the manipulation process, you need to clear the concept of how string workers in C.

What is strings in C?

Strings are typically used to represent text data. Unlike some other programming languages, C does not have a built-in string data type. Instead, strings are typically implemented as arrays of characters, terminated by a null character ('\0'). This null character is used to mark the end of the string.

String Declaration:

char myString[20];

* A string of up to 19 characters plus the null terminator.

String Initialization:

char myString[] = "Hello, World!";

* A string containing the characters "Hello, World!" along with the null terminator.

String Input:

char myString[20];
printf("Enter your name: ");
fgets(myString, sizeof(myString), stdin);

It uses the 'fgets' function to read a line of text from the standard input (keyboard) and stores it in the 'myString' array. 'fgets' reads up to 'sizeof(myString)' characters, which is 20 in this case, including any spaces, and stops reading when it encounters a newline character or reaches the specified limit.

* After reading the input, it prints the message "Your name is: " using 'printf'.

Output:

c Copy Code
#include <stdio.h>

int main(){
    char myString[20];
    printf("Enter your name: ");
    fgets(myString, sizeof(myString), stdin);

    printf("Your name is: %s",myString);
    return 0;
}
Output:
Enter your name: Ayan Sarkar
Your name is: Ayan Sarkar

* stdin: The standard input stream (keyboard) from which the input will be read.

string.h

In C programming, "string.h" is a standard library header file that provides various functions and macros for working with strings, which are sequences of characters. This header file is part of the C Standard Library and is commonly used when dealing with character arrays (strings) in C.

Some of the commonly used functions and macros provided by "string.h" include:

strcpy

Copies one string to another.

strcpy(char destination, char source);

strcat

Concatenates (appends) one string to another.

strcat(char destination, char source);

strlen

Returns the length of a string.

strlen(char str);

strcmp

Compares two strings whether they are equal or not.

strcmp(char str1, char str2);

strstr

Searches for a substring within a string.

strstr(char data, char find);

strtok

Splits a string into tokens based on a delimiter.

strtok(char str, char delimiter);

Example:

Let's see an example using the "string.h" header file in C:

c Copy Code
#include <stdio.h>
#include <string.h>

int main()
{
    char input[] = "Desktop,Laptop,Mobile";
    const char *delimiter = ",";

    // Tokenize the input string using strtok
    char *token = strtok(input, delimiter);

    while (token != NULL)
    {
        printf("Token: %s\n", token);
        token = strtok(NULL, delimiter);
    }

    return 0;
}
Output:
Token: Desktop
Token: Laptop
Token: Mobile

Note: The asterisk (*) symbol is used to represent a pointer. When you use the asterisk in a variable declaration, it indicates that the variable is a pointer.

What is New?

We have just updated this website & still working on adding new content & updating existing features.