Loading your workspace…
Loading your workspace…
Write your own functions for string length, comparison and concatenation that receive their strings through pointer parameters, and use them on two input words.
#include <stdio.h>
int my_length(const char *s) {
int n = 0;
while (s[n] != '\0') {
n++;
}
return n;
}
int my_compare(const char *a, const char *b) {
int i = 0;
while (a[i] != '\0' && a[i] == b[i]) {
i++;
}
return (unsigned char)a[i] - (unsigned char)b[i];
}
void my_concat(char *dest, const char *src) {
int i = 0;
int j = 0;
while (dest[i] != '\0') {
i++;
}
while (src[j] != '\0') {
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
}
int main(void) {
char first[50], second[50], joined[100];
int cmp;
printf("Enter first word:\n");
scanf("%49s", first);
printf("Enter second word:\n");
scanf("%49s", second);
printf("Length of \"%s\" = %d\n", first, my_length(first));
printf("Length of \"%s\" = %d\n", second, my_length(second));
cmp = my_compare(first, second);
if (cmp < 0) {
printf("\"%s\" is less than \"%s\"\n", first, second);
} else if (cmp > 0) {
printf("\"%s\" is greater than \"%s\"\n", first, second);
} else {
printf("\"%s\" and \"%s\" are equal\n", first, second);
}
joined[0] = '\0';
my_concat(joined, first);
my_concat(joined, second);
printf("Concatenated: %s\n", joined);
return 0;
}
Input
Hello
WorldOutput
Enter first word:
Enter second word:
Length of "Hello" = 5
Length of "World" = 5
"Hello" is less than "World"
Concatenated: HelloWorldInput
apple
appleOutput
Enter first word:
Enter second word:
Length of "apple" = 5
Length of "apple" = 5
"apple" and "apple" are equal
Concatenated: appleappleThe functions take const char * for input strings and char * only for the destination, which is the one that changes. Because arrays are passed as pointers, my_concat changes the caller's array without returning it.
my_compare returns the difference of the first mismatching characters, which is the same convention as the library strcmp. Casting to unsigned char keeps the result correct for characters outside the ASCII range.
Edit the program and press Run. It is free and needs no sign-in.
Copy a sample and paste it into the STDIN box in the editor before you press Run.
Hello World
Expected output
Enter first word: Enter second word: Length of "Hello" = 5 Length of "World" = 5 "Hello" is less than "World" Concatenated: HelloWorld
apple apple
Expected output
Enter first word: Enter second word: Length of "apple" = 5 Length of "apple" = 5 "apple" and "apple" are equal Concatenated: appleapple
It writes past the existing text without checking the size, so the caller must reserve room for both strings and the terminator.
A negative value means the first string is smaller, zero means equal, and a positive value means it is larger.
Without it, the result would not end, and printf and later string functions would read past the end.
const documents that these functions only read them, and lets the compiler reject accidental writes.
The width stops the read after 49 characters, leaving room for the terminator in a 50-byte array.
Back to all VTU BPOPS103 lab programs.