Hi All,
I have written a C program to solve this problem but I am eager to know whether the same output can be obtained using sed or awk?
This is the input:
star
ferry
computer
symbol
prime
time
This is the output:
starferry
ferrycomputer
computersymbol
symbolprime
primetime
This is my code written in C.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<inttypes.h>
char *chomp ( char * );
int32_t main ( int32_t argc , char ** argv )
{
FILE *input_file = NULL;
input_file = fopen ( argv [ 1 ] ,"r" );
if ( input_file == NULL )
{
fprintf ( stderr , "input file open error\n" );
}
FILE *output_file = NULL;
output_file = fopen ( "concatenated_words.dat" , "w" );
if ( output_file == NULL )
{
fprintf ( stderr , "file write error\n" );
}
unsigned long int status_flag = 0;
char *word1 = NULL;
word1 = ( char * ) malloc ( 20 * sizeof ( char ) );
if ( word1 == NULL )
{
fprintf ( stderr , "malloc() memory allocation failure\n" );
}
char *word2 = NULL;
word2 = ( char * ) malloc ( 20 * sizeof ( char ) );
if ( word2 == NULL )
{
fprintf ( stderr , "malloc() memory allocation failure\n" );
}
while ( !feof ( input_file ) )
{
if ( status_flag == 0 )
{
fscanf ( input_file , "%s\n" , word1 );
}
memset ( word2 , 0 , strlen ( word2 ) );
fscanf ( input_file , "%s\n" , word2 );
word1 = chomp ( word1 );
word2 = chomp ( word2 );
fprintf ( output_file , "%s%s\n" , word1 , word2 );
status_flag ++;
memset ( word1 , 0 , strlen ( word1 ) );
strcpy ( word1 , word2 );
}
free ( word1 );
free ( word2 );
fclose ( input_file );
fclose ( output_file );
}
char *chomp ( char *word )
{
int32_t word_length = 0;
word_length = strlen ( word );
if ( word [ word_length ] == '\n' )
{
word [ word_length ] = '\0';
}
return ( word );
}
I am using gcc on Linux.