Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following functions, consistent with the prototypes shown: int strlen(

ID: 3685641 • Letter: W

Question

Write the following functions, consistent with the prototypes shown:

int strlen(char* s);
Given a string, return the number of characters in the string, NOT INCLUDING the null at the end.

char* strcpy(char* dest, const char* src);
The function will copy the string s2 into the string s1. Note the function returns the address of the string.

char* strcat(char* dest, const char* src);
The function concatenates the string s2 into the end of string s1. If s1 is "Hello " and s2 is "World" before, after calling strcat, s1 will be "Hello World"

Important:

Do NOT use the C library routines.

Do implement the functions EXACTLY as prototyped (i.e., no extra parameters, no fewer parameters, etc.)

You may use the functions as "building blocks".

Write main() functions to test and debug each of the above string functions, using gets() and puts() for string input and output as follows:

To demonstrate strlen(), input your first name using gets() and use printf to print the string followed by the number of characters in the string. An input of "Ken" would print 3. Show this with 3 different string input examples.

To demonstrate strcpy(), input your first name using gets(s1) and use strcpy() to copy it to a second string, s2 and print both strings with puts(). So an input of "Ken" would print: "string1 = Ken string2 = Ken".

To demonstrate strcat(), use gets() to input your first name, last name, and Red ID as three separate strings, print them out, one per line, concatenate the three strings, and then use puts() to print out Hello followed by your name and Red ID. If the three input strings entered are "Ken" "Arnold" and "876543210", then it should print:

Explanation / Answer

Code:

#include <stdio.h>
#include <stdlib.h>

char* strcpy(char* dest, const char* src) //function for strcpy
{
char *from = src;
char *to = dest;

while(*from != '') //reading untill end of string
{
*to = *from;
to++;
from++;
}
*to = ''; //append null to end the string
}

char* strcat(char* dest, const char* src)
{
while (*dest!= '') //increment pointer untill ends of destination
*dest++ ;
*dest++ = ' ';
do
{
*dest++ = *src++; //copying src into dest char by char
}
while (*src != '') ;
*dest = '';
}
int strlen(char* s)
{
int length=0; // it will store the length of string
while(s[length]!='')
{
length++;
}
return length;
}
int main(void)
{
char s[50];
char s1[50];
char s2[50];
char fname[50]; //use to store first name
char lname[50]; //use to store last name
char redid[50]; //use to store redid
printf("enter the string: ");
gets(s);
printf("string is: %s length is: %d ",s,strlen(s));
printf("enter the string: ");
gets(s1);
strcpy(s2,s1);
printf("string1: %s ",s1);
printf("string2: %s ",s2);
printf("enter you first name, last name and red id: ");
gets(fname);
gets(lname);
gets(redid);
puts(fname);
puts(lname);
puts(redid);
strcat(fname,lname); //appending first name with last name
strcat(fname,redid); //then appending redid into it
printf("hello %s",fname);

return 0;
}

Sample input and output:

enter the string: //for strlen
ken
string is: ken length is: 3
enter the string: //for strcpy
ken
string1: ken
string2: ken
enter you first name, last name and red id: //for strcat
ken
Arnold
876543210
ken
Arnold
876543210
hello ken Arnold 876543210