?
Solved

C String Contatenation

Posted on 2003-10-31
3
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 75 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 75 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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