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

String Manipulation

The document is a C program that demonstrates various string manipulation functions including length calculation, copying, concatenation, comparison, and reversing. It uses functions like strlen, strcpy, strncpy, strcat, strcmp, and strrev to manipulate and display strings. The sample output illustrates the results of these operations on the strings 'Computer' and 'Science'.

Uploaded by

prathibhakannan0
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)
20 views2 pages

String Manipulation

The document is a C program that demonstrates various string manipulation functions including length calculation, copying, concatenation, comparison, and reversing. It uses functions like strlen, strcpy, strncpy, strcat, strcmp, and strrev to manipulate and display strings. The sample output illustrates the results of these operations on the strings 'Computer' and 'Science'.

Uploaded by

prathibhakannan0
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

String manipulation

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

int main() {
char str1[50] = "Computer";
char str2[50] = "Science";
char str3[50];

/* 1. String Length */
printf("Length of str1 = %d\n", strlen(str1));

/* 2. String Copy */
strcpy(str3, str1);
printf("After strcpy, str3 = %s\n", str3);

/* 3. String Copy (limited) */


strncpy(str3, str2, 4);
str3[4] = '\0';
printf("After strncpy, str3 = %s\n", str3);

/* 4. String Concatenation */
strcat(str1, str2);
printf("After strcat, str1 = %s\n", str1);

/* 5. String Comparison */
if (strcmp(str1, str2) == 0)
printf("str1 and str2 are equal\n");
else
printf("str1 and str2 are not equal\n");

/* 6. String Reverse */
strrev(str2);
printf("Reversed str2 = %s\n", str2);

return 0;
}

Sample output:
Length of str1 = 8
After strcpy, str3 = Computer
After strncpy, str3 = Scie
After strcat, str1 = ComputerScience
str1 and str2 are not equal
Reversed str2 = ecneicS

You might also like