C Programming not C++ Write the following functions, consistent with the prototy
ID: 3688874 • Letter: C
Question
C Programming not C++
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* si, char* s2); The function will copy the string s2 into the string s1. Note the function returns the address of the string. char* strcatfchar* si, char* s2); 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: "stringl = Ken string2 = Ken". To demonstrate strcat(), use gets() to input your first name, last name, and 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 ID. If the three input strings entered are "Ken" "Arnold" and "876543210", then it should print:Explanation / Answer
/*
* Define three main function according to your requirement strlen, strcpy, strcat
* use put according to you requirement
*/
#include <stdio.h>
// this function use for get string lenght and return lenght
int strlen(char * s){
int c = 0;
for(int i=0; s[i] != 0;i++)
{
c++;
}
return c;
}
// this function use for s1 to s2 and return s2
char* strcpy(char * s1, char* s2){
int i;
for(i=0; s1[i]!=''; ++i)
{
s2[i]=s1[i];
}
s2[i]='';
return s2;
}
// this function use for concat s1 with s2
char* strcat(char * s1, char* s2){
int l;
for(l=0;s1[l]!='';l++);
for(int i=0;s2[i]!='';i++)
s1[l++]=s2[i];
s1[l]='';
return s1;
}
int main()
{
//char userInput[256];
char s1[256], s2[256], s5[256], s6[256];
// user input first name
printf ("Enter first name: ");
gets (s1);
// user input last name
printf ("Enter last name: ");
gets (s5);
// user input ID
printf ("Enter ID: ");
gets (s6);
// cout s1 lenght
int char_count = strlen(s1);
printf ("Number of char in string :- ");
printf ("%d ",char_count);
char* char_copy = strcpy(s1,s2);
// add string1 message here
char s3[256] = "String1= ";
// concat string1 message with s1 value
char* char_copys = strcat(s3,s1);
// add string2 message here
char s4[256] = " String2= ";
// concat string2 message with s2 value
char* char_copys2 = strcat(s4,char_copy);
// concat string1 message with string2 message
char* char_copys3 = strcat(char_copys,char_copys2);
// final out show here
printf ("So an input of %s would print:- ", s1);
puts(char_copys3);
// cretae hello before name
char s7[256] = "Hello ";
// concat hello message with first name
char* hello_msg = strcat(s7,s1);
// this code use for add space between first name and last name
char s8[256] = " ";
char* str_with_space = strcat(hello_msg,s8);
// concat hello message with last name
char* first_last = strcat(str_with_space,s5);
// this code use for add space between name and ID
char s9[256] = " ";
char* name_with_space = strcat(first_last,s9);
// concat with ID
char* user_list = strcat(name_with_space,s6);
// print first name
puts(s1);
// print last name
puts(s5);
// print ID
puts(s6);
// print final message here with puts
puts(user_list);
return 0;
}
User Input : -
Enter First name: ken
Enter last name: Arnold
Enter ID: 876543210
Output : -
Number of char in string :- 3
So an input of ken would print:- String1 = ken String2= ken
ken
Arnold
876543210
Hello ken Arnold 876543210
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.