0% found this document useful (0 votes)
7 views16 pages

Crop Production

The document provides an overview of string handling functions in C, detailing various functions from the <string.h> library such as strlen(), strcpy(), strcat(), strcmp(), and strtok(). Each function is described with its syntax, purpose, and example code demonstrating its usage. The document serves as a guide for performing common string operations in C programming.

Uploaded by

ragavik430
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)
7 views16 pages

Crop Production

The document provides an overview of string handling functions in C, detailing various functions from the <string.h> library such as strlen(), strcpy(), strcat(), strcmp(), and strtok(). Each function is described with its syntax, purpose, and example code demonstrating its usage. The document serves as a guide for performing common string operations in C programming.

Uploaded by

ragavik430
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/ 16

String handling functions

Strings in C are represented as arrays of


characters, with the last character being the null
terminator ('\0').
The C Standard Library provides several functions in
<string.h> for performing operations
like copying, concatenation, comparison, searching,
and more.

January 23, 2025 1


Strlen()

size_t strlen(const char *str);

Description:
•strlen() returns the number of characters in the string, excluding
the null terminator ('\0').

•The function iterates through the string until it finds the null
character.

•It returns a value of type size_t representing the length of the


string.

January 23, 2025 2


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

int main()
{
char str[] = "Hello, World!";
printf("Length of string: %zu\n", strlen(str)); //
Output: 13 return 0;
}

January 23, 2025 3


String Copy

syntax
Char* strcpy(char *dest, const char *src);
Description:
•strcpy() copies the content of the source string (src)
into the destination string (dest).
•The destination string must have enough space to hold
the copied string, including the null terminator.
•The function returns the destination string (dest).

January 23, 2025 4


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

int main()
{
char src[] = "Hello, C!";
char dest[50];
strcpy(dest, src); // Copies src to dest

printf("Source: %s\n", src); // Output: Hello, C!

printf("Destination: %s\n", dest); // Output:


Hello, C!
return 0;}
January 23, 2025 5
Strncpy()
#include <stdio.h>
#include <string.h>
int main()
{
char src[] = "Hello, C!";
char dest[10];
strncpy(dest, src, 5); // Copy first 5
characters
dest[5] = '\0'; // Ensure null termination
printf("Destination: %s\n", dest); // Output:
Hello
eturn 0;
January 23, 2025 6
Strcat()

#include <stdio.h>
#include <string.h>
int main()
{
char str1[50] = "Hello";
char str2[] = ", World!";
strcat(str1, str2); // Concatenates str2 to str1
printf("Concatenated string: %s\n", str1); // Outp
World!
return 0;
}
January 23, 2025 7
Strncat()

#include <stdio.h>
#include <string.h>
int main()
{
char str1[50] = "Hello";
char str2[] = ", C Programming!";
strncat(str1, str2, 4); // Concatenates first 4 chars of str2
str1
printf("Concatenated string: %s\n", str1); // Output: Hell
return 0;
}

January 23, 2025 8


Strcmp()
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
if (strcmp(str1, str2) == 0)
{
printf("Strings are equal.\n"); // Output: Strings
are equal.
}
else
{
printf("Strings are not equal.\n");
}
return 0;
}
January 23, 2025 9
Strncmp()

#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello";
char str2[] = "Hell";
if (strncmp(str1, str2, 4) == 0)
{
printf("First 4 characters are equal.\n");
// Output: First 4 characters are equal.
}
else
{
printf("First 4 characters are not equal.\n");
}
return 0;
January 23, 2025 10
Strchr()

#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello, World!";
char *ptr = strchr(str, 'o');
if (ptr != NULL)
{
printf("Found 'o' at position: %ld\n", ptr
- str);
// Output: 4
}
return 0;
January 23, 2025 11
Strchr()

#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello, World!";
char *ptr = strchr(str, 'o');
if (ptr != NULL)
{
printf("Found 'o' at position: %ld\n", ptr
- str);
// Output: 4
}
return 0;
January 23, 2025 12
Strstr()-Find first occurrence of substring

Syntax:
char *strstr(const char *haystack, const char
*needle);
Description:
•strstr() finds the first occurrence of the
substring needle in the string haystack.
•It returns a pointer to the first character of the found substring
or NULL if not found.

January 23, 2025 13


Strstr()
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello, World!";
char *ptr = strstr(str, "World");
if (ptr != NULL)
{
printf("Found 'World' at position: %ld\n", ptr
- str);
// Output: 7
}
return 0;
January 23, 2025 14
Strtok()- Tokenize a string

Syntax:
char *strtok(char *str, const char *delim);
Description:
•strtok() breaks a string into tokens using the specified delimiters
(delim).
•The first call to strtok() processes the string,
•and subsequent calls return the next tokens.
•Each token is separated by the characters in the delimiter string.

January 23, 2025 15


Strtok()- Tokenize a string

#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "This is a test string";
char *token = strtok(str, " ");
while (token != NULL) {
printf("Token: %s\n", token);
token = strtok(NULL, " "); // Subsequent calls to get
next token
}
return 0;
} January 23, 2025 16

You might also like