C String Functions


C String Functions

A string is a one-dimensional array of characters that ends with the null character '0.'

A null-terminated string, on the other hand, consists of the string's characters followed by a null.

The declaration and initialization that follow generate a string that contains the word "Hello."

To accommodate the null character at the end of the word "Hello," the character array containing the string must be one bigger than the number of characters in the word.

char greeting[6] = 'H', 'e', 'l', 'l', 'o', '0'; char greeting[6] = 'H', 'e', 'l', 'l', 'o', '0'; char greeting[6] = 'H',

This null character denotes the string's end.

Double quotations are usually used to surround strings.

Characters in C are surrounded by single quotations

Example for C string:

    char string[20] = {‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘2’, ‘r’, ‘e’, ‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘\0’};

    char string[20] = “fresh2refresh”;

    char string [] = “fresh2refresh”;

  • The only difference between the two declarations is that when we define char as "string[20], the string value is allocated 20 bytes of memory.
  • When we specify char as “string[],” memory space will be allocated according to the program's requirements during execution.
  • The String.h header file contains all of the string functions in C.
  • Below are all of the string functions
  • For a detailed overview and sample programmes, Click on each string function name below for detail description and example programs.

Example of string function:

#include<stdio.h> #include<conio.h> void main() { char name[20]; printf("Enter your name:"); scanf("%s",name); printf("your name is %s",name); getch(); }

output:

Enter your name:Rohit your name is Rohit