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

C String Manipulation Functions

The document contains a C program that implements several string manipulation functions, including calculating string length, concatenation, comparison, extraction of substrings, and reversal of strings. It also includes a main function that prompts the user for two strings and demonstrates the use of these functions. The program outputs the length of the first string, the concatenated result, comparison result, reversed first string, extracted substring, and the index of the second string within the first.

Uploaded by

anmolupadhyay090
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 views5 pages

C String Manipulation Functions

The document contains a C program that implements several string manipulation functions, including calculating string length, concatenation, comparison, extraction of substrings, and reversal of strings. It also includes a main function that prompts the user for two strings and demonstrates the use of these functions. The program outputs the length of the first string, the concatenated result, comparison result, reversed first string, extracted substring, and the index of the second string within the first.

Uploaded by

anmolupadhyay090
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

#include <stdio.

h>

int mystrlen(char str[]) {

int i = 0;

while (str[i] != '\0') {

i++;

return i;

void mystrcat(char str1[], char str2[], char result[]) {

int i = 0, j = 0;

while (str1[i] != '\0') {

result[j++] = str1[i++];

i = 0;

while (str2[i] != '\0') {

result[j++] = str2[i++];

result[j] = '\0';

int mystrcmp(char str1[], char str2[]) {

int i = 0;

while (str1[i] != '\0' || str2[i] != '\0') {

if (str1[i] == str2[i]) {
i++;

else {

if (str1[i] < str2[i])

return -1;

else

return 1;

return 0;

void mystr_extract(char str[], int start, int end, char result[]) {

int i = 0;

while (start <= end && str[start] != '\0') {

result[i++] = str[start++];

result[i] = '\0';

int my_str_substr(char *str, char *sub) {

int i = 0, j, temp;

while (str[i] != '\0') {

temp = i;

j = 0;
while (sub[j] != '\0' && str[temp] == sub[j]) {

temp++;

j++;

if (sub[j] == '\0') {

return i; // substring found

i++;

return -1; // not found

void str_rev(char str[]) {

int i = 0;

int len = mystrlen(str);

int j = len - 1;

char temp;

while (i < j) {

temp = str[j];

str[j] = str[i];

str[i] = temp;

i++;

j--;

int main() {
char str1[100], str2[100], result[200];

int start, end;

printf("Enter string 1: ");

scanf("%s", str1);

printf("Enter string 2: ");

scanf("%s", str2);

printf("\nLength of string1 = %d", mystrlen(str1));

mystrcat(str1, str2, result);

printf("\nConcatenation = %s", result);

printf("\nCompare result = %d", mystrcmp(str1, str2));

str_rev(str1);

printf("\nReversed string1 = %s", str1);

printf("\nEnter start and end index for substring: ");

scanf("%d %d", &start, &end);

mystr_extract(str2, start, end, result);

printf("Extracted substring = %s", result);

printf("\nSubstring index (str2 in str1) = %d\n", my_str_substr(str1, str2));

return 0;

You might also like