Solved

C String Contatenation

Posted on 2003-10-31
3
281 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 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

Technology Partners: 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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.

730 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