Solved

C String Contatenation

Posted on 2003-10-31
3
277 Views
Last Modified: 2010-04-15
Hi,
First time playing with C strings and I have a simple problem.  I am using fgets() to read from stdin: (fgets(input, 100, stdin)).  Say I input the line 'firstline' so fgets will read 'firstline' into the input.  Now I want to concatenate something else - say 'blah' (not from stdin) onto the input.  The problem I am having is that the end of line character is forcing the concatenation onto the next line so i have:

firstline
blah

What i want to have is:

firstlineblah

What are some possible ways around this?  Thanks.
0
Comment
Question by:aomega
3 Comments
 

Author Comment

by:aomega
ID: 9656285
Never mind I used the following piece of code to eliminate '\n'.

if ((ptr = strchr(input, '\n')))
    *ptr = '\0';
0
 
LVL 6

Accepted Solution

by:
Ajar earned 25 total points
ID: 9656339
char  * temp = input;

while(*temp !='\n' &&*temp !='\r' &&*temp !='\0' )  temp++;//find the first occurance of  new line
if(temp == '\n' || temp == '\r' )  *temp='\0'; // terminate the string if the last character is newline

//now input is gurnanteed not to have the  new line..   because temp actually is using 'input'  variables' character array
 
//
0
 
LVL 9

Assisted Solution

by:tinchos
tinchos earned 25 total points
ID: 9657904
I would definitely suggest using the strcat function

Example

#include <string.h>        // For strcat and strcopy
#include <stdio.h>

void main( void )
{
   char string[80];
   strcpy( string, "Hello world from " );
   strcat( string, "strcpy " );
   strcat( string, "and " );
   strcat( string, "strcat!" );
   printf( "String = %s\n", string );
}

Output
String = Hello world from strcpy and strcat!

For more information
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_strcat.2c_.wcscat.2c_._mbscat.asp
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question