247R1A67L9 PPS Lab
247R1A67L9 PPS Lab
FUNCTIONS
Presented by:
Ch.Manisha
247R1A67L9
CSD-D
STRING HANDLING FUNCTIONS:
String handling functions are a set of tools
provided by programming languages to
manipulate and process strings (sequences
of characters). These functions allow
developers to perform various operations like
searching, modifying, formatting, or analyzing
strings. Here’s a list of common string-
handling functions and their purposes:
In C, string handling functions are available in the
<string.h> library and are used to perform various operations on strings.
Below are four common string handling functions, each explained with an example
program and output.
1. strlen: Calculates the length of a string
Syntax:
int strlen(const char *str);
EXAMPLE PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("The length of the string '%s' is: %d\n", str, length);
return 0;
}
OUTPUT:
The length of the string 'Hello, World!' is: 13
2.strcpy: Copies one string to another
Syntax
char* strcpy(char* dest, const char* src);
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Programming in C";
char destination[50];
strcpy(destination, source);
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
return 0;
}
OUTPUT:
Source: Programming in C
Destination: Programming in C
3.strchr: Finds the first occurrence of a character in a string
Syntax
char *strchr(const char *str, int c);
EXAMPLE PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Programming in C";
char ch = 'g';
char *position = strchr(str, ch);
if (position != NULL) {
printf("The first occurrence of '%c' is at position: %ld\n", ch, position - str + 1);
} else {
printf("Character '%c' not found in the string.\n", ch);
}
return 0;
}
OUTPUT:
The first occurrence of 'g' is at position: 4
4.strstr: Finds the first occurrence of a substring in a string.
Syntax
char *strstr (const char *s1, const char *s2);
EXAMPLE PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Learning C programming is fun!";
char substr[] = "C programming";
char *position = strstr(str, substr);
if (position != NULL) {
printf("Substring found at position: %ld\n", position - str + 1);
} else {
printf("Substring not found.\n");
}
return 0;
}
OUTPUT:
Substring found at position: 10
Advantages of String Handling Functions
1.Ease of Use
1. String handling functions simplify operations like searching, slicing,
and formatting, which would otherwise require extensive manual
coding.
2.Time-Saving
1. Predefined functions reduce development time as they are
optimized and ready to use.
3.Code Readability
1. Using string functions makes the code cleaner and more
understandable, improving maintainability.
4.Consistency
1. These functions behave consistently across different use cases,
minimizing errors.
5.Performance Optimization
1. Built-in string functions are often implemented in the core of the
language, making them faster and more efficient than custom
implementations.
6.Error Handling
1. Many string functions come with built-in mechanisms to handle
common errors, like invalid indexes or non-existent substrings.
7.Portability
1. String functions are usually consistent across platforms in the same
language, ensuring that code behaves the same way regardless of
Disadvantages of String Handling Functions
1.Language Dependency
•Different programming languages may have different names, behaviors, or syntaxes for
string functions, leading to a learning curve when switching between languages.
2.Limited Customization
•Predefined functions may not meet complex or specific needs, requiring developers to
write custom implementations.
3.Potential Performance Overhead
•In some cases, built-in functions might include additional processing (e.g., validation
checks) that can introduce a slight overhead in performance-critical applications.
4.Memory Usage
•String functions often create new strings instead of modifying existing ones (due to
immutability in many languages like Python or Java), potentially leading to higher
memory consumption.
5.Hidden Complexity
•The internal implementation of string functions may be complex, making debugging
tricky if something doesn’t work as expected.
6.Black Box Nature
•Developers might rely on these functions without fully understanding their internals,
which could lead to issues in edge cases.
7.Error Handling Variability
•Some functions handle errors differently across languages (e.g., find() vs. index() in Python), potentially
causing unexpected behavior.
Conclusion:
String handling functions in c are power tools for managing text-
based data ,but they must be used with Caution to avoid common
pitfalls Such as buffer overflows and incorrect memory
management .Modern best practices recommend using safer
alternatives and checking for buffer sizes to prevent
vulnerabilities . Understanding these functions and using them
appropriately ensures efficient and reliable string operations in c
program.
String functions are the built-in functions declared in <string.h>
header file that can be used to perform various operations on the
strings such as string copying, concatenation, comparison,
searching, and more.
3.strupr( )