//-----------------------------------------------
// C Programming Language K&R
// str len cpy cat array
// 30926 ver 1
// tested works 3~1-9-26
//-----------------------------------------------
#include <stdio.h>
#define STORE 50
//-----------------------------------------------
void strcat_array(char dest[], char src[]) {
// strcat_array(dest,src);
int i = 0, j = 0;
// Find the end of dest
while (dest[i] != '\0') {
i++;
}
// Copy src to the end of dest
while ((dest[i++] = src[j++]) != '\0') {
// Continue copying until '\0' is reached
}
}
void strcpy_array(char dest[], char src[]) {
// strcpy_array(dest,src);
int i = 0;
while ((dest[i] = src[i]) != '\0') {
i++;
}
}
//-----------------------------------------------
int strlen_array(char s[]) {
// =strlen_array(s);
int i = 0;
while (s[i] != '\0') {
i++;
}
return i;
}
//-----------------------------------------------
//-----------------------------------------------
void strcat_array1(const int destsize, char dest[], const int srcsize,const char src[]) {
//strcat_array1(destsize, dest, srcsize, src[]);
int i = 0;
int j = 0;
// 1. Find the null-terminator of dest, ensuring we don't exceed destsize
while (i < destsize && dest[i] != '\0') {
i++;
}
// If dest is already full (no room for null terminator), exit early
if (i >= destsize) {
return;
}
// 2. Copy characters from src to dest, respecting both buffer limits
// We need space for characters AND the final '\0' (hence i < destsize - 1)
while (j < srcsize && src[j] != '\0' && i < destsize - 1) {
dest[i] = src[j];
i++;
j++;
}
// 3. Ensure the resulting string is properly null-terminated
dest[i] = '\0';
}
//-----------------------------------------------
void strcpy_array1(const int destsize, char dest[], const int srcsize, const char src[]) {
//strcpy_array1(destsize, dest, srcsize, src[]);
int i = 0;
// Ensure we do not read past src or write past dest,
// and leave room for the null terminator.
while (i < srcsize && i < destsize - 1 && src[i] != '\0') {
dest[i] = src[i];
i++;
}
// Always safely null-terminate the destination string
if (destsize > 0) {
dest[i] = '\0';
}
}
//-----------------------------------------------
int strlen_array1(const int srcsize,const char src[]) {
// =strlen_array1(scrsize,src);
int i = 0;
while (i < srcsize && src[i] != '\0') {
i++;
}
return i; // Returns at most srcsize
}
//-----------------------------------------------
void strfrountend(char str[]){
// strfrountend(str);
char dest[STORE]="";
printf("K&R close to original examples\n");
printf("strlen strcpy strcat array ver\n");
printf("Test string: %s\n",str);
printf("\n");
int i=strlen_array(str);
printf("len of str: %d\n",i);
strcpy_array(dest,str);
printf("cpy to dest from str: %s\n", dest);
strcat_array(dest,str);
printf("cat to dest from str: %s\n", dest);
printf("\n");
}
//-----------------------------------------------
void strfrountend1(int strsize, char str[]){
//strfrountend(strsize,str);
char dest[STORE]="";
printf("K&R more robust examples\n");
printf("strlen strcpy strcat array1 ver\n");
printf("Test string str: %s\n",str);
printf("\n");
int i = strlen_array1(strsize,str);
printf("len of str: %d\n",i);
// Calling the function cpy cat
strcpy_array1(sizeof(dest), dest, strsize, str);
// Printing the result to generate an output
printf("cpy to dest from str: %s\n", dest);
strcat_array1(sizeof(dest), dest, strsize, str);
// Printing the result to generate an output
printf("cat to dest from str: %s\n", dest);
printf("\n");
}
//-----------------------------------------------
int main() {
char str[STORE]="123456789A";
strfrountend1(sizeof(str),str);
strfrountend(str);
return 0;
}
//-----------------------------------------------
To embed this project on your website, copy the following code and paste it into your website's HTML: