1,105,546 Community Members

Concatenation - append 2nd half of a string to another

Member Avatar
student2012
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need to prompt the user to enter two strings and then create a third string with the first half of string1 and the second half of string2. I was able to put the first half of string1 into string3 but I cannot figure out how to put the second half of string2 into string 3. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

int main ()
{	
	char string1[255], string2[255], string3[255];
	int string1Len = 0, string2Len = 0;
	
	printf ("Enter String 1: ");
	fgets (string1, 255, stdin);
	string1[strlen(string1) - 1] = '\0';
	string1Len = strlen(string1);

	printf ("Enter String 2: ");
	fgets (string2, 255, stdin);
	string2[strlen(string2) - 1] = '\0';
	string2Len = strlen(string2);
	
	printf ("String 1 is %d bytes long and String 2 is %d bytes long.\n", string1Len, string2Len);
	
	strncpy (string3, string1, string1Len / 2);
	string3[string1Len / 2] = '\0';
	strcat (string3, " ");
		
	strncat (string3, string2, string2Len / 2);    
	printf ("String 3 is: %s\n", string3);

    system("pause");
    	
	return 0;
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

strcat (string3, &string2[start-of-second-half]); This passes the address of the position of string2 you want to add.

Member Avatar
student2012
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

strcat (string3, &string2[start-of-second-half]); This passes the address of the position of string2 you want to add.

What do you put if the position of the beginning of the second half is unknown, since this is from user input?

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

Is the second half of string2 preceded by a space?

Member Avatar
student2012
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Is the second half of string2 preceded by a space?

Yes I put one in.

strcat (string3, " ");
Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

Do you just need to copy 1 word or is it a sentence that you need to copy the whole second half of the sentence?

well you could do it like this
use strtok for the string 2 and set space as the delimeter
once it's at its second iteration then you could copy the content to string3

Member Avatar
mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: -10 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
-1
 

why you use strcat() method....you can try use loop read char by char and place in third string

Member Avatar
student2012
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

why you use strcat() method....you can try use loop read char by char and place in third string

what is the syntax for that?

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

why you use strcat() method....you can try use loop read char by char and place in third string

My guess is that the purpose of the function is to concatinate strings, which is also (surprisingly) the object of the assignment.

What do you put if the position of the beginning of the second half is unknown, since this is from user input?

If you don't know where "the position of the beginning of the second half is, you can't do the program. I assume you need to figure out where the second half starts.

Member Avatar
student2012
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

My guess is that the purpose of the function is to concatinate strings, which is also (surprisingly) the object of the assignment.


If you don't know where "the position of the beginning of the second half is, you can't do the program. I assume you need to figure out where the second half starts.

Any suggestions as to how I go about that. I've read the chapter over a dozen times and done a ton of research online but I still can't figure out what I'm missing.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Given: sentence 1 = "This it the time to fly"
Given: sentence 2 = ""To err is human, to really foul things up takes a computer"

What is the resulting sentence supposed to be? Not by computer. By definition of the assignment.

Member Avatar
student2012
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Given: sentence 1 = "This it the time to fly"
Given: sentence 2 = ""To err is human, to really foul things up takes a computer"

What is the resulting sentence supposed to be? Not by computer. By definition of the assignment.

First half of string 1 would be: This is the
Second half of string 2 would be: ul things up takes a computer

It would display as: "This it the ul things up takes a computer"

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

First half of string 1 would be: This is the
Second half of string 2 would be: ul things up takes a computer

It would display as: "This it the ul things up takes a computer"

How do you figure this?
What did you do to arrive at "This is the" and "ul things up takes a computer"?
Put that into code...

How long is a sentence? How can you figure that out in code?
What's half a sentence?
etc, etc...
'Tis simplicity itself when you stop thinking in terms of the end product, and think in terms of one step at a time.

Remember, a journey of a thousand miles begins with a single step. Then another. And another. Until you drop 10 feet before the gate in exhaustion. No wait -- that's not how it goes. :icon_wink:

Member Avatar
shanki himanshu
Junior Poster
130 posts since Dec 2010
Reputation Points: 27 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

strcat (string3, &string2[start-of-second-half]); This passes the address of the position of string2 you want to add.

start of second half can be calculated by using strlen and half of it.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: