Strings and string functions In C - Passionate Geekz

Breaking

Where you can unleash your inner geek.

Thursday, 5 September 2019

Strings and string functions In C

What is String in C ?

String is a collection of characters. There are two ways to declare string in c language.

1. By char array

2. By string literal

  • Header file

#include<string.h> // used to perform string functions

  • Declaring string by char array
char c[4]={'g','o','o','d'};

 

  • Declaring string by string literal
char c[]="morning";

Example 

[php]#include <stdio.h>
#include <conio.h>
void main ()
{
char c1[11]={‘g’,’o’,’o’,d’};
char c2[]="morning";
printf("Char Array Value : %s\n", c1);
printf("String Literal Value : %s\n", c2);
getch();
}[/php]

Output

[code]Char Array Value: good  Literal Value is: morning[/code]

String Functions

No.FunctionDescription
1)strlen(string_name)Returns the length of string name.
2)strcpy(destination, source)Copies the contents of source string to destination string.
3)strcat(first_string, second_string)Concats or joins first string with second string. The result of the string is stored in first string.
4)strcmp(first_string, second_string)Compares the first string with second string. If both strings are same, it returns 0.

No comments:

Post a Comment