943,916 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9392
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 28th, 2008
0

How to merge 2 strings ?

Expand Post »
Hi.

I am trying to merge 2 chars. in C but i don't know how to do it.

My code:

  1. char dd[1], aa[2];
  2.  
  3. dd[0] = "2";
  4. dd[1] = "3";
  5.  
  6. aa[] = dd[0] + dd[1];

So it aa[] should show: 23 but it gives error. Maybe i am doing it wrong. Please help. Also this is just demo code, i am actually inputting the values of dd[0] and dd[1] from user using getche() function

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kikloo is offline Offline
6 posts
since Dec 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

Quote ...
char dd[1];
That's wrong. You're allocating memory for only one character. Should be dd[2].

Quote ...
aa[] = dd[0] + dd[1];
This statement adds the ASCII values of '2' and '3'. So your result will be 50(ASCII value of 2) + 51(ASCII value of 3). And the result can't be just assigned to an entire array.

If you just wanna merge two chars, you can use:
  1. aa[0] = dd[0];
  2. aa[1] = dd[1];
But I'm not sure if that's what you're looking for. Maybe more info about what you're trying to do would help.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

You use the standard C functions strcat()
Last edited by Aia; Dec 28th, 2008 at 2:57 pm. Reason: Wrong function link
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 28th, 2008
0

Re: How to merge 2 strings ?

Hi,

I am using a custom function in which I have made so that the user can input only 1 char. and that is also just a numeric char. so meaning that user can only enter from 0 to 9.

I am doing like following:

  1. char dd[2];
  2.  
  3. dd[0] = getinp(); // say user enetered 2
  4. dd[1] = getinp(); // say user enetered 3

Now I have to merge the both values so that it becomes 23 and I can check whether the value is less than 30 or not. How to achieve this ?

If i could just do the following:

  1. aa = dd[0] + dd[1]; // aa will then have 23
  2. if (aa > 30)
  3. {
  4. printf ("Value should not be more than 30");
  5. }
  6. else
  7. {
  8. printf ("Thanks");
  9. }

This is really what I am trying to do.

I am using a custom function becuase it does'nt allows a user to enter anything else other than numbers.

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kikloo is offline Offline
6 posts
since Dec 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

If dd[0] and dd[1] contain the character they entered, the character is NOT the numeric value. (For example in ASCII '0' is 48)

So if the example is dd[0] = '2' and dd[1] = '3' then aa = dd[0] + dd[1]; would give aa the value 50 + 51 or 101.

If you want the numeric value I usually do something like (dd[0] - '0') which in our case would be 2.

So if we did that as we went, we could write
  1. aa = (dd[0] - '0') + (dd[1] - '0')

There, now aa is 2 + 3 which makes aa 5. (wait, didn't you say it should be 23? maybe I missed something.)

I guess I could have done:
  1. aa = 10 * (dd[0] - '0') + (dd[1] - '0')

That would be 23.

You could probably even do it without the array of characters (unless you needed them for something else):
  1. aa = (getinp() - '0');
  2. aa = 10 * aa + (getinp() - '0');
Last edited by Murtan; Dec 28th, 2008 at 3:20 pm.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

Hi,

When I added the line:

  1. aa = 10 * (dd[0] - '0') + (dd[1] - '0');

It gives me error:
Lvalue required

What does that means ?

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kikloo is offline Offline
6 posts
since Dec 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

  1. char onetwo[3]; /* for one, two, zero chars */
  2. char one, two;
  3. one = getche();
  4. two = getche();
  5. onetwo[0] = one; /* array indicies started from 0 */
  6. onetwo[1] = two;
  7. onetwo[2] = '\0'; /* c-string terminated zero byte */
  8. /* Now you have well-formed c-string */
  9. printf("echo: %s\n",onetwo);
Remember: a single char is not a string in C, a c-string is an array of chars with zero byte at the end of its contents.
Last edited by ArkM; Dec 28th, 2008 at 3:30 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

HI,

I still don't get it. Please help me merge the string or chars. so that I can go ahead with the program

I am a php and VB programmer and its very easy to do this task in those languages.

VB: a = a & b
php: a = a . b;

What the syntax to do it in C ?

Arrrgh.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kikloo is offline Offline
6 posts
since Dec 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

Your original program declared aa as an array of characters:
  1. char dd[1], aa[2];

In your most recent post, you treated aa as an integer:
  1. aa = dd[0] + dd[1]; // aa will then have 23
  2. if (aa > 30)

So I did too. It is not permitted to assign to the array.

Change your declaration to:
  1. char dd[2];
  2. int aa;
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 28th, 2008
0

Re: How to merge 2 strings ?

>What does that means ?
It means you need to learn a little be more about array of chars and what they represent.
As far as the compiler is concerned aa is not a proper variable to re-assign the kind effect resulted from 10 * (dd[0] - '0') + (dd[1] - '0')
Click to Expand / Collapse  Quote originally posted by ArkM ...
one = getche();
two = getche();
No reason to use a non-portable function when getchar() could have do.
Last edited by Aia; Dec 28th, 2008 at 3:50 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: File input output please help :(
Next Thread in C Forum Timeline: Understanding pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC