STRING FUNCTION - Programming in C.pptx

13 views 22 slides Dec 28, 2023
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Programming in C - String Functions


Slide Content

STRING FUNCTION C PROGRAMMING

DEFINITION In simple language STRING'S are nothing but the character array. The declaration of string (character array) is much similar to normal array declaration. Each string is terminated by '\0' as indication of string termination.

STRING DECLARATION & INITILIZATION - C char str1[]={‘ a ',‘p', p ‘,'l','e '.}; char str2[]="Programming spark";

PROGRAM EXAMPLE void main() { char name[30]; printf ("Enter your name"); scanf ("%s", name); //format specifier printf ("%s", name); //format specifier }

OUTPUT Enter your name apple

OUTPUT Enter your name apple apple

STRING LIBRARY FUNCTIONS strlen strcpy strcat strrev Strcmp strncmp strupr strlwr strncat

STRING FUNCTION - EXAMPLE LENGTH This function accepts string as parameter and return integer i.e the length of String passed to it. Example #include < stdio.h > #include < string.h > void main(void) { char string[]="spark"; int len ; len = strlen (string); printf ("length of %s is %d\t", string, len ); }

STRING FUNCTION - EXAMPLE LENGTH This function accepts string as parameter and return integer i.e the length of String passed to it. Example #include < stdio.h > #include < string.h > void main(void) { char string[]="spark"; int len ; len = strlen (string); printf ("length of %s is %d\t", string, len ); } Output::length of spark is 5

STRING FUNCTION - EXAMPLE COPY This function accepts 2 strings as parameter,1st one is destination string and 2nd is source string. This function copies source string to destination string. Example #include < stdio.h > #include < string.h > void main(void) { char src []="spark", dest [15]; strcpy ( dest,src ); printf ("%s is copied to dest string\t", dest ); }

STRING FUNCTION - EXAMPLE COPY This function accepts 2 strings as parameter,1st one is destination string and 2nd is source string. This function copies source string to destination string. Example #include < stdio.h > #include < string.h > void main(void) { char src []="spark", dest [15]; strcpy ( dest,src ); printf ("%s is copied to dest string\t", dest ); } Output: spark is copied to dest string.

STRING FUNCTION - EXAMPLE CONCATENATION This function accepts two strings source string is appended to the destination string. Example #include < stdio.h > #include < string.h > void main(void) { char src []="spark", dest []="programming"; strcat ( dest,src ); printf ("concatenated string is %s", dest ); }

STRING FUNCTION - EXAMPLE CONCATENATION This function accepts two strings source string is appended to the destination string. Example #include < stdio.h > #include < string.h > void main(void) { char src []="spark", dest []="programming"; strcat ( dest,src ); printf ("concatenated string is %s", dest ); } Output: concatenated string is programmingspark

STRING FUNCTION - EXAMPLE REVERSE This function accepts single string as parameter and reverse that string. Example #include < stdio.h > #include < string.h > void main(void) { char string[]="spark"; strrev (string); printf ("reverse string is % s",string ); }

STRING FUNCTION - EXAMPLE REVERSE This function accepts single string as parameter and reverse that string. Example #include < stdio.h > #include < string.h > void main(void) { char string[]="spark"; strrev (string); printf ("reverse string is % s",string ); } Output: reverse string is kraps .

STRING FUNCTION - EXAMPLE STRING COMPARASION This function is similar to strcmp ().The onlyy difference is that it ignores the case.example SparK and spark both are same. Example #include < stdio.h > #include < string.h > void main(void) { char string1[]="spark",string2[]=" SPArk "; int cmp ; cmp = strcmpi (string1,string2); if( cmp >0)

STRING FUNCTION - EXAMPLE printf ("%s > %s",string1,string2); else { if( cmp <0) printf ("%s < %s",string1,string2); else printf ("%s = %s",string1,string2); } }

STRING FUNCTION - EXAMPLE printf ("%s > %s",string1,string2); else { if( cmp <0) printf ("%s < %s",string1,string2); else printf ("%s = %s",string1,string2); } } Output: spark = SPArk

STRING FUNCTION - EXAMPLE LOWERCASE This function accepts single string that can be in any case(lower or upper).It converts the string in lower case. Example #include < stdio.h > #include < string.h > void main(void) { char string1[]=" SPArk "; strlwr (string1); printf ("%s is in lower case",string1); }

STRING FUNCTION - EXAMPLE LOWERCASE This function accepts single string that can be in any case(lower or upper).It converts the string in lower case. Example #include < stdio.h > #include < string.h > void main(void) { char string1[]=" SPArk "; strlwr (string1); printf ("%s is in lower case",string1); } Output: spark is in lower case.

STRING FUNCTION - EXAMPLE UPPERCASE This function accepts single string that can be in any case(lower or upper). Itconverts the string in upper case. Example #include < stdio.h > #include < string.h > void main(void) { char string1[]=" SPArk "; strupr (string1); printf ("%s is in upper case",string1); }

STRING FUNCTION - EXAMPLE UPPERCASE This function accepts single string that can be in any case(lower or upper). Itconverts the string in upper case. Example #include < stdio.h > #include < string.h > void main(void) { char string1[]=" SPArk "; strupr (string1); printf ("%s is in upper case",string1); } Output: SPARK is in upper case.
Tags