Read two strings from a user to perform concatenate and length operations of a string including and excluding space. Write a program to handle string operations using pointers as reference to the function and without using library functions.
Runtime Input :
Pointers
Are Easy
Output :
Pointers Are Easy
Length of String Including Spaces =17
Length of String Excluding Spaces = 15
#include <stdio.h>
void concat(char* str1, const char* str2) {
while (*str1++) ; /*find end of the string 1*/
str1--;
*str1++ = ' '; /* add a space */
while (*str1++ = *str2++); /* Copy second string */
}
int length(const char *str) {
int len=0;
while (*str++) len++;
return len;
}
int length_exclude_space(const char *str) {
int len=0;
while (*str) {
if (*str++ != ' ') {
len++;
}
}
return len;
}
int main()
{
char str1[1024];
char str2[1024];
int len;
gets(str1);
gets(str2);
concat(str1, str2);
printf("%s\n", str1);
len = length(str1);
printf("Length of String Including Sapces = %d\n", len);
len = length_exclude_space(str1);
printf("Length of String Excluding Sapces = %d\n", len);
return 0;
}
Comments
Leave a comment