Using C program. char* strCat (char *str, char *str2) - Concatenate the two stri
ID: 3853990 • Letter: U
Question
Using C program. char* strCat (char *str, char *str2) - Concatenate the two strings "str" and "str2" and store that in a newly created dynamic string. The function should return this dynamic string. You cannot use the concatenation functions. For example, a call to this function on the string "Hello", and "World" will return a dynamically created string that contains "Hello World". void consonant Vowel(char *str) - This function will print out the number of consonants and vowels in the string. For example, a call to this function on the string "Hello", will print Consonants: 3, Vowels 2. What you will submit via handin: string_utils.h and string_utils.c.Explanation / Answer
Answer for your first task
#include <stdio.h>
#include <stdlib.h>
int strLen(char* str)
{
int cnt = 0;
while(1)
{
if(str[cnt] == 0)
break;
cnt++;
}
return cnt;
}
char* strCat(char* str1, char* str2)
{
char* ch;
ch = (char*)malloc(sizeof(char) * (strLen(str1) + strLen(str2)+1));
int i = 0;
while(1)
{
if(str1[i] == 0)
{
i = 0;
break;
}
ch[i] = str1[i];
i++;
}
while(1)
{
if(str2[i] == 0)
break;
ch[strLen(str1) + i] = str2[i];
i++;
}
return ch;
}
void main()
{
char* ch;
char* str1 = "hello";
char* str2 = "world";
ch = strCat(str1, str2);
printf("%s ", ch);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.