0% found this document useful (0 votes)
3 views2 pages

String Function

The document describes various C string functions including strcat() for concatenation, strlen() for calculating string length, strcmp() for comparing strings, and strcpy() for copying strings. Each function is accompanied by its syntax and a sample program demonstrating its usage. The document provides outputs for each example to illustrate the results of the string operations.

Uploaded by

tobi030611
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

String Function

The document describes various C string functions including strcat() for concatenation, strlen() for calculating string length, strcmp() for comparing strings, and strcpy() for copying strings. Each function is accompanied by its syntax and a sample program demonstrating its usage. The document provides outputs for each example to illustrate the results of the string operations.

Uploaded by

tobi030611
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

The C string functions are built-in functions that can be used for various operations and

manipulations on strings. These string functions can be used to perform tasks such as string
copy, concatenation, comparison, length, etc.
1. strcat() Function:
The strcat() function in C is used for string concatenation. This function is used to
concatenate two strings.
Syntax of strcat(): char* strcat (char* dest, const char* src);
Program:
#include <stdio.h>
#include <string.h>
Int main(){
char str1[20] = "Hello ";
char str2[] = "World!";
strcat(str1, str2);
printf("%s", str1);
return 0;
}
Output: Hello World!

2. strlen() Function
The strlen() function calculates the length of a given string.
Syntax: int strlen(const char *str);
Program:
#include <stdio. h>
#include <string.h>
int main() {
char alphabet[] = "ABCDEFGH IJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
return 0;
}
OUTPUT: 26

[Link](): This function is used to compare two strings.


Syntax of strcmp(): int strcmp(const char *str1, const char *str2);
#include <stdio.h>
#include<string. h>
Int main(){
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
printf("%d\n", strcmp(str1, str2));
printf("%d\n", strcmp(str1, str3));
return 0;
}
Output:
0
-32
[Link]():The strcat() function in C is used for string [Link] is used to copy the
string
Syntax:
char* strncpy( char* dest, const char* src, size_t n );
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "Hello World!";
char dest[20];
strcpy(dest, source);
printf("Source: %s\n", source);
printf("Destination: %s\n", dest);
return 0;
}
Output:
Source: Hello World!
Destination: Hello World!

You might also like