Answer to Question #206496 in C for Steve

Question #206496

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


1
Expert's answer
2021-06-13T10:24:46-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS