[go: up one dir, main page]

0% found this document useful (0 votes)
18 views15 pages

PL1 Lecture 9 String

Uploaded by

simmxd0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

PL1 Lecture 9 String

Uploaded by

simmxd0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

STRING IN C LANGUAGE

Course Title: Programming language 1

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 09 Week No: 10 Semester: Fall 23-24


Lecturer: Md. Faruk Abdullah Al Sohan; faruk.sohan@aiub.edu
Lecture Outline

 List of topics,
 The concept of string
 Examples of string
 String Functions
The concept of STRING

 C Strings are nothing, but array of characters ended with null


character (‘\0’).
 This null character indicates the end of the string.

 Strings are always enclosed by double quotes.Whereas, character is


enclosed by single quotes in C.
Examples of STRING declaration
 char string[20] = {‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘2’, ‘r’, ‘e’, ‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘\0’};
(or)
 char string[20] = “fresh2refresh”;
(or)
 char string [] = “fresh2refresh”;
Difference between above declarations are, when we declare char as
“string[20]”, 20 bytes of memory space is allocated for holding the string
value.
When we declare char as “string[]”, memory space will be allocated as per
the requirement during execution of the program.
Example

#include <stdio.h>

int main ()
{
char string[20] = "this is a
string";

printf("The string is : %s \n",


string );
return 0;
}
STRING Functions
String.h header file supports all the string functions in C language.

String functions Description


strcat ( ) Concatenates str2 at the end of str1
strncat ( ) Appends a portion of string to another
strcpy ( ) Copies str2 into str1
strncpy ( ) Copies given number of characters of one string to another
strlen ( ) Gives the length of str1
strcmp ( ) Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if
str1 > str2
strcmpi ( ) Same as strcmp() function. But this function negotiates case. “A” and
“a” are treated as same.
STRING Functions
String.h header file supports all the string functions in C language.

String functions Description


strchr ( ) Returns pointer to first occurrence of char in str1
strrchr ( ) last occurrence of given character in a string is found
strstr ( ) Returns pointer to first occurrence of str2 in str1
strrstr ( ) Returns pointer to last occurrence of str2 in str1
strdup ( ) Duplicates the string
strlwr ( ) Converts string to lowercase
strupr ( ) Converts string to uppercase
strrev ( ) Reverses the given string
strset ( ) Sets all character in a string to given character
strnset ( ) It sets the portion of characters in a string to given character
strtok ( ) Tokenizing given string using delimiter
STRCAT()

#include <stdio.h>
#include <string.h>

int main( )
{
char source[ ] = "
C course" ;
char target[ ]= "
C tutorial" ;

printf ( "\nSource Source string = C course


string =( target,
strcat %s", source ) ; Target string = C tutorial
source ) ; Target string after strcat( ) = C tutorial C course
printf(("\nTarget
printf "\nTargetstring after strcat( ) = %s", target ) ;
} string = %s",
target ) ;
STRNCAT()

#include <stdio.h>
#include <string.h>

int main( )
{
char source[ ] = " C course" ;
char target[ ]= "C tutorial" ;

printf ( "\nSource string = %s", source ) ;


printf ( "\nTarget string = %s", target ) ;
Source string = C course
strncat ( target, source, 5 ) ;
Target string = C tutorial
Target string after strcat( ) = C tutorial C co
printf ( "\nTarget string after strncat( ) = %s",
target );

}
STRCPY()

#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = " C Programming" ;
char target[20]= "Hello" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strcpy ( target, source ) ;
printf ( "\ntarget string after strcpy( ) = %s", Source string = C Programming
target ) ; return 0; Target string = Hello
}
Target string after strcpy( ) = C Programming
STRNCPY()

#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = "Programming" ;
char target[20]= "Hello" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strncpy ( target, source, 3) ; source string = Programming
printf ( "\ntarget string after strncpy( ) = %s", target string = Hello
target ) ; return 0; target string after strncpy( ) = Prolo
}
STRLEN()

#include
<stdio.h>
#include
<string.h>

int main( )
{
int len;
char
array[20]=”
printf
C ( "\string length = %d \n" , String length = 13
len );
Programming"
} return
; 0;

len =
strlen(array) ;
STRCMP()

•strcmp( ) function in C compares two given strings and returns zero if they are same.

•If length of string1 < string2, it returns < 0 value. If length of string1 > string2,
it returns > 0 value. Syntax for strcmp( ) function is given below.
STRCMP()

#include
<stdio.h>
#include
<string.h> int
main( )
{
char str1[ ] =
”This" ;
char str2[ ] =
”is C
Programming
" ; Output:
int i, j, k ; 0
i = strcmp -1
( str1, 1
”This" ) ;
j = strcmp
( str1, str2 ) ;
k = strcmp
( str1, ”T" ) ;
STRNCMP()

#include <stdio.h>
#include <string.h>
int main( )
{
char str1[ ] = "This is a Program";
char str2[ ] = "This is C
Programming";
int i, j;
i = strncmp ( str1, str2, 9) ;
j = strncmp ( str1, str2, 8 ) ;
Output:
printf ( "\n%d %d", i, j) ; 1
return 0; 0
}

You might also like