Joins two strings with strcat() function - C String

C examples for String:String Function

Description

Joins two strings with strcat() function

Demo Code

#include <stdio.h>
#include <string.h>  /* declares the strcat() function */
#define SIZE 80/* ww  w.  j ava 2 s .com*/

char * s_gets(char * st, int n);

int main(void){
    char flower[SIZE];
    char addon[] = "this is a test.";
    
    puts("What is your favorite flower?");
    if (s_gets(flower, SIZE))
    {
        strcat(flower, addon);
        puts(flower);
        puts(addon);
    }
    else
        puts("End of file encountered!");
    puts("bye");
    return 0;
}

char * s_gets(char * st, int n){
    char * ret_val;
    int i = 0;
    
    ret_val = fgets(st, n, stdin);
    if (ret_val){
        while (st[i] != '\n' && st[i] != '\0')
            i++;
        if (st[i] == '\n')
            st[i] = '\0';
        else // must have words[i] == '\0'
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

Result


Related Tutorials