| | |
How to merge 2 strings ?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
Hi.
I am trying to merge 2 chars. in C but i don't know how to do it.
My code:
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.
I am trying to merge 2 chars. in C but i don't know how to do it.
My code:
C Syntax (Toggle Plain Text)
char dd[1], aa[2]; dd[0] = "2"; dd[1] = "3"; 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.
•
•
•
•
char dd[1];
•
•
•
•
aa[] = dd[0] + dd[1];
If you just wanna merge two chars, you can use:
C Syntax (Toggle Plain Text)
aa[0] = dd[0]; aa[1] = dd[1];
You use the standard C functions strcat()
Last edited by Aia; Dec 28th, 2008 at 2:57 pm. Reason: Wrong function link
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
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:
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
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:
C Syntax (Toggle Plain Text)
char dd[2]; dd[0] = getinp(); // say user enetered 2 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:
C Syntax (Toggle Plain Text)
aa = dd[0] + dd[1]; // aa will then have 23 if (aa > 30) { printf ("Value should not be more than 30"); } else { printf ("Thanks"); }
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
•
•
Join Date: May 2008
Posts: 570
Reputation:
Solved Threads: 93
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
If you want the numeric value I usually do something like
So if we did that as we went, we could write
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:
That would be 23.
You could probably even do it without the array of characters (unless you needed them for something else):
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
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
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):
c Syntax (Toggle Plain Text)
aa = (getinp() - '0'); aa = 10 * aa + (getinp() - '0');
Last edited by Murtan; Dec 28th, 2008 at 3:20 pm.
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
Hi,
When I added the line:
It gives me error:
Lvalue required
What does that means ?
Thanks.
When I added the line:
C Syntax (Toggle Plain Text)
aa = 10 * (dd[0] - '0') + (dd[1] - '0');
It gives me error:
Lvalue required
What does that means ?
Thanks.
c Syntax (Toggle Plain Text)
char onetwo[3]; /* for one, two, zero chars */ char one, two; one = getche(); two = getche(); onetwo[0] = one; /* array indicies started from 0 */ onetwo[1] = two; onetwo[2] = '\0'; /* c-string terminated zero byte */ /* Now you have well-formed c-string */ printf("echo: %s\n",onetwo);
Last edited by ArkM; Dec 28th, 2008 at 3:30 pm.
•
•
Join Date: May 2008
Posts: 570
Reputation:
Solved Threads: 93
Your original program declared aa as an array of characters:
In your most recent post, you treated aa as an integer:
So I did too. It is not permitted to assign to the array.
Change your declaration to:
c Syntax (Toggle Plain Text)
char dd[1], aa[2];
In your most recent post, you treated aa as an integer:
c Syntax (Toggle Plain Text)
aa = dd[0] + dd[1]; // aa will then have 23 if (aa > 30)
So I did too. It is not permitted to assign to the array.
Change your declaration to:
c Syntax (Toggle Plain Text)
char dd[2]; int aa;
>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
No reason to use a non-portable function when getchar() could have do.
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') No reason to use a non-portable function when getchar() could have do.
Last edited by Aia; Dec 28th, 2008 at 3:50 pm.
![]() |
Similar Threads
- i got this project and i need someone to help design it (C)
- Internet speeds way down along with connectivity (Viruses, Spyware and other Nasties)
- A crazy problem(at least to me) (C++)
- Merge Two Files (C++)
- Help needed :( (C++)
- little help :( (C++)
- Copying words into an array of char ?!? (C)
- Macros & VB to create various letters (Visual Basic 4 / 5 / 6)
- Question about SELECT statement. (MySQL)
- String Addition (C++)
Other Threads in the C Forum
- Previous Thread: File input output please help :(
- Next Thread: Understanding pointers
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable voidmain() win32api windows.h






