Unit 10 - V1
Unit 10 - V1
C PROGRAMMING
Unit 10
String Part 2
Table of Contents
1. INTRODUCTION
1.1. Objectives:
2. OPERATIONS ON STRINGS
Operations on strings in C encompass a variety of functionalities vital for manipulating
textual data efficiently. These include initialization of strings using character arrays or string
literals, input/output operations for reading from and writing to streams, concatenation to
merge multiple strings, and length calculation with functions like strlen(). String copying is
achieved using strcpy() or strncpy(), while comparison utilizes strcmp(). String searching,
extraction, and tokenization involve functions such as strstr(), strchr(), and strtok().
Modification operations include character replacement, whitespace removal, and content
insertion. Case conversion alters character cases with functions like toupper() and tolower().
Finally, string formatting constructs formatted strings from other data types using sprintf()
and snprintf(), and parses input strings using scanf() and sscanf(). These operations equip
programmers with essential tools to effectively handle and process textual data in C
programming, facilitating tasks ranging from simple text manipulation to complex data
parsing and analysis.
Finding a Substring:
To find a substring within a string, we can use functions like strstr() from the standard
library. This function searches for the occurrence of a substring within a given string and
returns a pointer to the first occurrence if found, or NULL if not found.
#include <stdio.h>
#include <string.h>
int main() {
if (substr != NULL) {
} else {
return 0;
This program demonstrates the usage of strstr() function to find a substring ("world") within
the string "hello world".
We can iterate through each character of a string using a loop and check for specific
characters using conditional statements like if. Alternatively, functions like strchr() and
strrchr() can be used to find the first or last occurrence of a character in a string, respectively.
#include <stdio.h>
#include <string.h>
int main() {
if (ch != NULL) {
} else {
return 0;
Here, strchr() function is used to find the first occurrence of character 'o' in the string "hello
world".
Tokenizing a String:
Tokenizing involves breaking a string into smaller parts, or tokens, based on delimiters such
as spaces or punctuation marks. The strtok() function from the standard library can be used
to tokenize a string. It returns a pointer to the next token found in the string and modifies
the original string to mark the end of the token.
#include <stdio.h>
#include <string.h>
int main() {
printf("%s\n", token);
return 0;
This program tokenizes the string "apple,banana,orange" using strtok() function and prints
each token (separated by comma).
We can extract substrings from a string using array indexing or functions like memcpy() or
strncpy(). These functions allow you to copy a specified number of characters from one
location in memory to another, effectively extracting the desired substring.
#include <stdio.h>
#include <string.h>
int main() {
char substr[6];
substr[5] = '\0';
return 0;
In this example, strncpy() function is used to extract a substring of length 5 starting from
index 6 of the string "hello world".
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
str[i] = toupper(str[i]);
str[i] = tolower(str[i]);
return 0;
This program demonstrates how to convert a string to uppercase and then to lowercase
using toupper() and tolower() functions. These functions take a character as input and
return the corresponding uppercase or lowercase equivalent. By iterating through each
character of the string, you can apply these functions to perform case conversion.
#include <stdio.h>
#include <string.h>
int main() {
// Replace a character
// Remove whitespace
while (str[src]) {
str[dst++] = str[src];
src++;
return 0;
In this C Program:
We insert the string " Welcome" at the end using the strcat() function.
This demonstrates how to perform various string modification operations in C to achieve the
desired changes in the string. Depending on the specific requirements, you may need to use
different techniques and functions for string modification.
sprintf(): This function formats and stores a series of characters and values in a string. It is
similar to printf(), but instead of printing to the standard output, it writes to a string.
snprintf(): Similar to sprintf(), snprintf() formats and stores characters and values in a
string, but with additional functionality to specify the maximum number of characters to be
written.
scanf(): This function reads formatted input from the standard input stream (usually the
keyboard) and assigns the values to variables based on the specified format string.
sscanf(): Similar to scanf(), sscanf() reads formatted input from a string instead of the
standard input stream. It parses the input string according to the specified format and
assigns the extracted values to variables.void insertionSort(int arr[], int n)
#include <stdio.h>
int main() {
char str[100];
int input;
scanf("%d", &input);
char name[20];
int age;
return 0;
This c program showcases the basic usage of sprintf(), snprintf(), scanf(), and sscanf() for
string formatting and parsing in C programming.
SELF-ASSESSMENT QUESTIONS - 1
1. What function is used to read formatted input from a string in C?
(a) sscanf()
(b) scanf()
(c) fscanf()
(d) sprint()
2. Which function is used to tokenize a string into smaller strings
based on delimiters in C?
(a) strsep()
(b) strtok()
(c) strspn()
(d) strcspn()
Operation Function
String Length
Calculation strlen()
String
Concatenation strcat()
These string handling functions are declared in the "string.h" header file.
String Length Calculation: To calculate the length of a string in C, you can use the strlen()
function from the <string.h> header.
#include <stdio.h>
#include <string.h>
int main() {
return 0;
String Concatenation: To concatenate two strings in C, you can use the strcat() function
from the <string.h> header. Here is an example:
#include <stdio.h>
#include <string.h>
int main() {
strcat(str1, str2);
return 0;
String Copy: To copy one string to another in C, you can use the strcpy() function from the
<string.h> header.
#include <stdio.h>
#include <string.h>
int main() {
char dest[20];
strcpy(dest, src);
return 0;
String Comparison: To compare two strings in C, you can use the strcmp() function from
the <string.h> header.
#include <stdio.h>
#include <string.h>
int main() {
if (result == 0) {
} else {
return 0;
String Reversal:
#include <stdio.h>
#include <string.h>
int main() {
printf("%c", str[i]);
printf("\n");
return 0;
The code reverses the string "Hello, World!" by iterating backward through its characters
and printing them, resulting in the reversed string "dlroW ,olleH".
Substring Extraction:
#include <stdio.h>
#include <string.h>
int main() {
char sub[20];
sub[5] = '\0';
return 0;
Finding Substring: To find a substring within a string in C, you can use the strstr() function
from the <string.h> header.
#include <stdio.h>
#include <string.h>
int main() {
if (substr != NULL) {
} else {
return 0;
Case Conversion: To convert the case of characters in a string in C, we can use the standard
library functions toupper() and tolower().
#include <stdio.h>
#include <ctype.h>
int main() {
str[i] = toupper(str[i]);
return 0;
SELF-ASSESSMENT QUESTIONS – 2
4. Which function is used for calculating the length of a string in C?
(a) strlength()
(b) lengthstr()
(c) strlen()
(d) strcount()
5. Which function is used for extracting a substring from a string in C?
(a) subextract()
(b) strsubstr()
(c) substr()
(d) strstr()
6. Which functions are used for case conversion of characters in C?
(a) toupper() and tolower()
(b) convertcase() and casechange()
(c) changecase() and casemanipulate()
(d) switchcase() and transformcase()
4. SUMMARY
String searching and extraction tasks involve finding substrings within strings, locating
specific characters, tokenizing strings based on delimiters, and extracting substrings based
on specified positions and lengths. String case conversion functions, including toupper() and
tolower(), enable the transformation of characters to uppercase and lowercase, respectively,
facilitating versatile text processing. String modification operations encompass various tasks
such as appending, replacing, or removing characters, allowing for flexible string
manipulation. String formatting functions like sprintf() and snprintf() facilitate precise
string representation, while scanf() and sscanf() enable reading formatted input from
strings, enhancing input processing capabilities. Additional string operations include
determining string lengths without relying on library functions, concatenating strings,
copying string contents, comparing strings, reversing string characters, extracting
substrings based on specified indices, and finding substrings within strings. Mastering these
operations equips C programmers with essential skills for developing efficient and reliable
software solutions across diverse domains, enhancing code readability, efficiency, and
functionality. Understanding and effectively utilizing these string operations are crucial for
developing robust software solutions that meet specific project requirements.
5. TERMINAL QUESTIONS
1. Explain the difference between strchr() and strstr() functions in C with respect to
string searching.
2. Explain the purpose of the toupper() and tolower() functions in C. Provide an
example.
3. Describe the process of modifying a string in C. Provide an example demonstrating a
modification operation.
1. (a) sscanf()
2. (b) strtok()
3. (a) strstr()
4. (c) strlen()
5. (c) substr()
6. (a) toupper() and tolower()