Crop Production
Crop Production
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.
int main()
{
char str[] = "Hello, World!";
printf("Length of string: %zu\n", strlen(str)); //
Output: 13 return 0;
}
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).
int main()
{
char src[] = "Hello, C!";
char dest[50];
strcpy(dest, src); // Copies src to dest
#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;
}
#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.
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.
#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