[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

C String Contatenation

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
aomega
Asked:
aomega
2 Solutions
 
aomegaAuthor Commented:
Never mind I used the following piece of code to eliminate '\n'.

if ((ptr = strchr(input, '\n')))
    *ptr = '\0';
0
 
AjarCommented:
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
 
tinchosCommented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now