0% found this document useful (0 votes)
3 views

Program1

The document provides C/C++ programs to implement string manipulation functions without using the string.h header file. It includes custom implementations for functions to get the length of a string, concatenate two strings, copy one string to another, and compare two strings, both without and with pointers. The main function demonstrates the usage of these custom functions with user input.

Uploaded by

rosaro4033
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Program1

The document provides C/C++ programs to implement string manipulation functions without using the string.h header file. It includes custom implementations for functions to get the length of a string, concatenate two strings, copy one string to another, and compare two strings, both without and with pointers. The main function demonstrates the usage of these custom functions with user input.

Uploaded by

rosaro4033
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q.

1 Write a program in C or C++ language for the following functions


without using string.h header file:

a: "to get the length of a string, you use the strlen() function"

b: "To concatenate (combine) two strings, you can use the strcat() function

c: "To copy the value of one string to another, you can use the strcpy()"

d: "To compare two strings, you can use the strcmp() function." and other
related functions

Solution: Without using pointers:

#include <stdio.h>

// Function to get the length of a string

int my_strlen(char str[]) {

int length = 0;

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

length++;

return length;

// Function to concatenate two strings

void my_strcat(char dest[], char src[]) {

int i = 0;

// Find the end of the destination string

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

i++;

}
int j = 0;

// Append the source string to the destination string

while (src[j] != '\0') {

dest[i] = src[j];

i++;

j++;

// Add the null-terminator

dest[i] = '\0';

// Function to copy one string to another

void my_strcpy(char dest[], char src[]) {

int i = 0;

// Copy characters from source to destination

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

dest[i] = src[i];

i++;

// Add null-terminator

dest[i] = '\0';

// Function to compare two strings

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

int i = 0;
// Compare the strings character by character

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

if (str1[i] != str2[i]) {

return str1[i] - str2[i]; // Return the difference between first non-


matching characters

i++;

// If one string is shorter than the other, return the difference in length

return str1[i] - str2[i];

int main() {

// Test data

char str1[100], str2[100], str3[100], str4[100];

// Input two strings

printf("Enter first string: ");

fgets(str1, sizeof(str1), stdin);

// Remove the newline character from fgets if present

str1[my_strlen(str1) - 1] = '\0';

printf("Enter second string: ");

fgets(str2, sizeof(str2), stdin);

str2[my_strlen(str2) - 1] = '\0';

// Part a: Get the length of a string


printf("\nLength of first string: %d\n", my_strlen(str1));

// Part b: Concatenate two strings

my_strcpy(str3, str1); // Copy str1 to str3

my_strcat(str3, str2); // Concatenate str2 to str3

printf("\nConcatenated string: %s\n", str3);

// Part c: Copy one string to another

my_strcpy(str4, str1);

printf("\nCopied string: %s\n", str4);

// Part d: Compare two strings

int result = my_strcmp(str1, str2);

if (result == 0) {

printf("\nStrings are equal.\n");

} else if (result < 0) {

printf("\nFirst string is lexicographically smaller than the second.\n");

} else {

printf("\nFirst string is lexicographically greater than the second.\n");

return 0;

With Using Pointers:

#include <stdio.h>
// Function to get the length of a string int my_strlen(const char *str) { int
length = 0; while (str[length] != '\0') { length++; } return length; }

// Function to concatenate two strings void my_strcat(char *dest, const char


*src) { // Move the pointer to the end of the destination string while (*dest !=
'\0') { dest++; }

// Append the source string to the destination string


while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
// Add the null-terminator at the end of the concatenated string
*dest = '\0';

// Function to copy one string to another void my_strcpy(char *dest, const


char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\
0'; // Ensure null-termination }

// Function to compare two strings int my_strcmp(const char *str1, const char
*str2) { while (*str1 != '\0' && *str2 != '\0') { if (*str1 != *str2) { return *str1
- *str2; // Return the difference between the first non-matching characters }
str1++; str2++; } return *str1 - *str2; // If one string is longer, this will return
the difference }

int main() { // Test data char str1[100], str2[100], str3[100], str4[100];

// Input two strings


printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
// Remove the newline character from fgets if present
str1[my_strlen(str1) - 1] = '\0';

printf("Enter second string: ");


fgets(str2, sizeof(str2), stdin);
str2[my_strlen(str2) - 1] = '\0';
// Part a: Get the length of a string
printf("\nLength of first string: %d\n", my_strlen(str1));

// Part b: Concatenate two strings


my_strcpy(str3, str1); // Copy str1 to str3
my_strcat(str3, str2); // Concatenate str2 to str3
printf("\nConcatenated string: %s\n", str3);

// Part c: Copy one string to another


my_strcpy(str4, str1);
printf("\nCopied string: %s\n", str4);

// Part d: Compare two strings


int result = my_strcmp(str1, str2);
if (result == 0) {
printf("\nStrings are equal.\n");
} else if (result < 0) {
printf("\nFirst string is lexicographically smaller than the second.\n");
} else {
printf("\nFirst string is lexicographically greater than the second.\n");
}

return 0;
}

You might also like