Solved

C String Contatenation

Posted on 2003-10-31
3
234 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now