Professional Documents
Culture Documents
Mr. B. ASHOK
Assistant Professor
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
• Strings in C
String manipulation functions like strlen, strcpy, strcat, etc., are available in the C
standard library (string.h) to perform operations on strings.
String literals in C are enclosed in double quotes (e.g., "Hello, World!"). They are
automatically null-terminated and can be assigned to character arrays or used with
pointers to access and manipulate strings.
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
String Declaration
• Syntax:
char string_variable_name [size];
3) Using the strcpy function: Note: Make sure the size of the character
char str[10]; array is sufficient to hold the string,
strcpy(str, "Hello"); including the null character.
scanf() function is used to read input from the user and the printf() function to
display the string. Here's an example:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("The entered string is: %s\n", str); Note: The %s format specifier in scanf
return 0; reads a string until it encounters
} whitespace, so it may not be suitable for
reading strings with spaces. To read a
string with spaces, fgets() function is used
instead.
int precision = 8;
int biggerPrecision = 16;
char greetings[] = "Hello world";
printf("|%.8s|\n", greetings);
|Hello wo|
printf("|%.*s|\n", precision , greetings);
|Hello wo|
printf("|%16s|\n", greetings); | Hello world|
printf("|%-16s|\n", greetings); |Hello world |
printf("|%*s|\n", biggerPrecision , greetings); | Hello world|
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
String Library Functions
The predefined functions that are designed to handle strings are available
in the library “string.h”. They are
Function Description
strlen() Calculates the length of a string
strcpy() Copies a string from source to destination
strcat() Concatenates two strings
strcmp() Compares two strings lexicographically
strstr() Used to search for a substring within a string
The strlen() is used to calculate the length of a string. It returns the number
of characters in the string, excluding the null character ('\0') at the end.
Syntax:
Example:
The strcpy() is used to copy a string from the source to the destination. It
copies each character of the source string to the destination string until it
encounters the null character ('\0') that marks the end of the string.
Syntax:
Example:
Example:
The strcmp() function is used to compare two strings. The function takes
two string arguments and returns an integer value that indicates the
relationship between the two strings.
Syntax:
int strcmp(const char *str1, const char *str2);
Example:
char str1[] = "Baahubali";
char str2[] = "Baahubali";
int result = strcmp(str1, str2);
if (result < 0)
printf("'%s' is lexicographically smaller than '%s'\n", str1, str2);
else if (result > 0)
printf("'%s' is lexicographically greater than '%s'\n", str1, str2);
else
printf("'%s' is lexicographically equal to '%s'\n", str1, str2);
'Baahubali' is lexicographically equal to 'Baahubali'
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
strstr() function
The strstr() function is used to search for a substring within a given string.
Syntax:
char *strstr(const char *haystack, const char *needle);
Example:
char s1[] = "ignorance is bliss";
char s2[] = "is";
String found
char* p; First occurrence of string 'is' in 'ignorance is bliss' is
p = strstr(s1, s2); 'is bliss'
if (p) {
printf("String found\n");
printf("First occurrence of string '%s' in '%s' is '%s'", s2, s1, p);
} else
printf("String not found\n");
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
Summary
Team – CTSD