0% found this document useful (0 votes)
14 views

String

c programming

Uploaded by

ambika venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

String

c programming

Uploaded by

ambika venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

String Manipulation Functions

Function Description
strlen() finds the length of string
strcat( ) concatenates two strings
strcmp( ) compares two strings
strcpy( ) copies one string in to another string
strncpy( ) copies up to n characters from the one string to another
strncmp( ) compares at most the first n bytes of two strings

strchr() searches for the first occurrence of the character in the


string
strstr() finds the first occurrence of the substring in the string

string.h
strlen()

 Calculates the length of the string up to but not including the null
character
 Syntax:
size_t strlen(const char *str);
size_t- This is the unsigned integral type
str − This is the string whose length is to be found
 This function returns the length of string
//Ex: program to Illustrate/Demonstrate strlen()
#include<string.h>
#include<stdio.h>
void main()
{
char str[]="hello";
int len;
len=strlen(str);
printf("The length of the string is:%d\n",len);
}
strcat()

 appends the string pointed to by str2 to the end of the string pointed
to by str1
 Syntax:

char *strcat(char *str1, const char *str2);


 The terminating null character of str1 is overwritten
 The process stops when the terminating null character of str2 is
encountered
 str1 should be big enough to stores the contents of str2
// program to Illustrate/Demonstrate strcat()

#include<string.h>

#include<stdio.h>

void main()

char str1[20]="hello",str2[7]="World";

strcat(str1,str2);

printf("str1=%s",str1);

}
strcmp()

 compares the string pointed to, by str1 to the string pointed to by str2.
 syntax

int strcmp(const char *str1, const char *str2)


 This function return values that are as follows −

- if Return value < 0 then it indicates str1 is less than str2.

- if Return value > 0 then it indicates str2 is less than str1.

- if Return value = 0 then it indicates str1 is equal to str2.


// program to illustrate / demonstrate strcmp()
#include<string.h>
#include<stdio.h>
void main()
{
char str1[7]="a",str2[7]="b";
int res=strcmp(str1,str2);
if(res==0)
printf("strings are equal");
else if(res>0)
printf("str1 is greater than str2");
else
printf("str1 is lesser than str2");
}
strcpy()

 copies the string pointed to by str2 to str1 including the null


character
 Syntax:

char * strcpy(char *str1, const char str2);


 Str1 should be big enough to store the contents of str2
// program to illustrate/demonstrate strcpy()

#include<string.h>

#include<stdio.h>

void main()

char str1[20],str2[7]="World";

strcpy(str1,str2);

printf("str1=%s",str1);

}
strncpy()

 copies up to n characters from the string pointed to, by str2 to str1


 Syntax:

char *strncpy(char *str1, const char *str2, size_t n);

- str1 − This is the pointer to the destination array where the


content is to be copied.

- str2 − This is the string to be copied.

- n − The number of characters to be copied from source.


// program to illustrate/ demonstrate strncpy()
#include<string.h>
#include<stdio.h>
void main()
{
char str1[20],str2[7]="World";
strncpy(str1,str2,2);
str1[2]='\0';
printf("str1=%s",str1);
}
strncmp()

 compares at most the first n characters of str1 and str2.


 Syntax:

int strncmp(const char *str1, const char *str2, size_t n)


- str1 − This is the first string to be compared.
- str2 − This is the second string to be compared.
- n − The maximum number of characters to be compared.
// program to illustrate / demonstrate strncmp()
#include<string.h>
#include<stdio.h>
void main()
{
char str1[7]="Hello",str2[7]="Help";
if(strncmp(str1,str2,2)==0)
printf("the two strings are identical");
else
printf("the two strings are not identical");
}
strchr()

 searches for the first occurrence of the character c in the string


pointed to by the argument str.
 Syntax:

char *strchr(const char *str, int c)

-str −This is the C string to be scanned.

-c − This is the character to be searched in str.

- This function returns a pointer to the first occurrence of the character c


in the string str, or NULL if the character is not found.
// program to illustrate / demonstrate strchr()
#include<string.h>
#include<stdio.h>
void main()
{
char str[50]="hello welcome to the world of c";
char *pos;
pos=strchr(str,'l');
if(pos)
printf("\n l found in str at position%d",pos-str+1);
else
printf("n is not in str");
}
strstr()

 finds the first occurrence of the substring str2 in the string str1.
 The terminating '\0' characters are not compared.
 Syntax:

char *strstr(const char *str1, const char *str2);

- str1− This is the main C string to be scanned.

- str2 − This is the small string to be searched within str1


 This function returns a pointer to the first occurrence in str1of any of
the entire sequence of characters specified in str2, or a null pointer if
the sequence is not present
// program to illustrate / demonstrate strstr()
#include<string.h>
#include<stdio.h>
void main()
{
char str1[50]="hello welcome to the world of c";
char str2[10]="welcome";
char *ptr;
ptr=strstr(str1,str2);
if(ptr)
printf("substring found");
else
printf("substring not found");
}

You might also like