How to merge 2 strings ?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 6
Reputation: kikloo is an unknown quantity at this point 
Solved Threads: 0
kikloo kikloo is offline Offline
Newbie Poster

How to merge 2 strings ?

 
0
  #1
Dec 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: How to merge 2 strings ?

 
0
  #2
Dec 28th, 2008
char dd[1];
That's wrong. You're allocating memory for only one character. Should be dd[2].

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: How to merge 2 strings ?

 
0
  #3
Dec 28th, 2008
You use the standard C functions strcat()
Last edited by Aia; Dec 28th, 2008 at 2:57 pm. Reason: Wrong function link
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: kikloo is an unknown quantity at this point 
Solved Threads: 0
kikloo kikloo is offline Offline
Newbie Poster

Re: How to merge 2 strings ?

 
0
  #4
Dec 28th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 570
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: How to merge 2 strings ?

 
0
  #5
Dec 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: kikloo is an unknown quantity at this point 
Solved Threads: 0
kikloo kikloo is offline Offline
Newbie Poster

Re: How to merge 2 strings ?

 
0
  #6
Dec 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to merge 2 strings ?

 
0
  #7
Dec 28th, 2008
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: kikloo is an unknown quantity at this point 
Solved Threads: 0
kikloo kikloo is offline Offline
Newbie Poster

Re: How to merge 2 strings ?

 
0
  #8
Dec 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 570
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: How to merge 2 strings ?

 
0
  #9
Dec 28th, 2008
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;
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: How to merge 2 strings ?

 
0
  #10
Dec 28th, 2008
>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')
Originally Posted by ArkM View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC