String Handling Functions
21CSC2T411: Programming in C
SIMMI S
Assistant Professor
Department Of Computer Science(UG)
Kristu Jayanti College,Autonomous
Bengaluru -560077,India
Strings
The string can be defined as the one-dimensional array of characters
terminated by a null ('0') character.
The character array or the string is used to manipulate text such as
word or sentences.
The termination character ('0') is important in a string since it is the
only way to identify where the string ends.
Simmi S,Department of Computer Science
Syntax: char string_name[size];
char name[10]; //Declaration
char name[]=“rose”; //Declaration and Initialization
char name[]={‘r’,’o’,’s’,’e’,’0’} //Declaration and Initialization
Declaring String
Simmi S,Department of Computer Science
String Handling Functions
• The C string functions are built-in functions that can be used for
various operations and manipulations on strings.
• These string functions can be used to perform tasks such as string
copy, concatenation, comparison, length, etc.
• The string handling functions are defined in a header file
called string.h.
Simmi S,Department of Computer Science
Types Of String Handling Functions
Function Syntax (or) Example Description
strcpy() strcpy(string1, string2) Copies string2 value into string1
strncpy() strncpy(string1, string2, 5) Copies first 5 characters string2 into string1
strlen() strlen(string1) Returns total number of characters in string1
strcat() strcat(string1,string2) Appends string2 to string1
strncat() strncpy(string1, string2, 4) Appends first 4 characters of string2 to string1
strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if
string1>string2
strncmp() strncmp(string1, string2, 4) Compares first 4 characters of both string1 and
string2
Simmi S,Department of Computer Science
strlen()
Simmi S,Department of Computer Science
strcpy() & strncpy()
Simmi S,Department of Computer Science
strcmp() & strncmp()
Simmi S,Department of Computer Science
strcat() & strcat()
Simmi S,Department of Computer Science
Simmi S,Department of Computer Science
Thank You

STRING HANDLING FUNCTIONS.pdf

  • 1.
    String Handling Functions 21CSC2T411:Programming in C SIMMI S Assistant Professor Department Of Computer Science(UG) Kristu Jayanti College,Autonomous Bengaluru -560077,India
  • 2.
    Strings The string canbe defined as the one-dimensional array of characters terminated by a null ('0') character. The character array or the string is used to manipulate text such as word or sentences. The termination character ('0') is important in a string since it is the only way to identify where the string ends. Simmi S,Department of Computer Science
  • 3.
    Syntax: char string_name[size]; charname[10]; //Declaration char name[]=“rose”; //Declaration and Initialization char name[]={‘r’,’o’,’s’,’e’,’0’} //Declaration and Initialization Declaring String Simmi S,Department of Computer Science
  • 4.
    String Handling Functions •The C string functions are built-in functions that can be used for various operations and manipulations on strings. • These string functions can be used to perform tasks such as string copy, concatenation, comparison, length, etc. • The string handling functions are defined in a header file called string.h. Simmi S,Department of Computer Science
  • 5.
    Types Of StringHandling Functions Function Syntax (or) Example Description strcpy() strcpy(string1, string2) Copies string2 value into string1 strncpy() strncpy(string1, string2, 5) Copies first 5 characters string2 into string1 strlen() strlen(string1) Returns total number of characters in string1 strcat() strcat(string1,string2) Appends string2 to string1 strncat() strncpy(string1, string2, 4) Appends first 4 characters of string2 to string1 strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same; less than 0 if string1<string2; greater than 0 if string1>string2 strncmp() strncmp(string1, string2, 4) Compares first 4 characters of both string1 and string2 Simmi S,Department of Computer Science
  • 6.
  • 7.
    strcpy() & strncpy() SimmiS,Department of Computer Science
  • 8.
    strcmp() & strncmp() SimmiS,Department of Computer Science
  • 9.
    strcat() & strcat() SimmiS,Department of Computer Science
  • 10.
    Simmi S,Department ofComputer Science Thank You