Strings in C || Learn C Language Onlinee
×


Strings in C

3079

 String in C Language is a chain of characters, with each character linked together.

 It has a special end mark called a null character '\0'.

They are also used for storing and manipulating text.

 To handle string, it is important to know basic operations and functions.

 you can create a string in two ways using a character array and by using string literals.

 One Fundamental operation is the string Initialization where you declare a character array and assign a string literal.

Representation of string 

 Each character in the string is stored in a contiguous memory location.

 The characters 'H', 'e', 'l', 'l', 'o' are stored sequentially.

 The null character '\0' marks the end of the string.

 Any characters beyond the null character are ignored when the string is processed.

 The diagram illustrates the string "Hello" with the null character '\0' indicating the end of the string.

Syntax of string in C Language 

char name_of_string [size];
For Example:

char str[4] = "C LANGUAGE";
C provides lots of functions to operate on string

1strlen(): strlen() is used to calculate the length of the string without counting the Null character at the end.

2strcpy(): Another function is strcpy() which is used to copy one string to another.

3strcat(): strcat() is used to join two strings.

4strcmp(): strcmp() is used to compare two strings

5strchr(): strchr() is used to find the occurrence of a character in a string.

6strstr(): is used to Find the first occurrence of a substring in a string.

These are just a few examples; there are more string functions available in C for various purposes.

Example :

// Program for string in C
#include <stdio.h>

int main()
{
// Declare a string
char message[7] = {'S', 'T', 'R', 'I', 'N', 'G','\0'};

// Print the string
printf("message: %s\n", message);
return 0;
}
Output:

message: STRING



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments