The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
String to int array conversion in C programming
Discuss String to int array conversion in C programming in the C Programming forum on Dev Shed. String to int array conversion in C programming C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 22nd, 2012, 12:46 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 1
Time spent in forums: 12 m 42 sec
Reputation Power: 0
|
|
|
String to int array conversion in C programming
Hi
I need to know how can I convert a string into an int array and then int array into string array.
void readArr(int MD[], int MQ[])
{
int i, b, val;
int array[4];
char string1[4];
char string2[4];
printf("\nEnter the value of Multiplicant: ");
gets(string1);
/* string1 is "0011" and I need it to convert as
array[4]={0,0,1,1} */
for (int i=0; i< string1.length; i++)
{
array[i] = Integer.ParseInt(string1[i]);
}
}
I am getting problem in my code. I don't know how to write a code for converting int array into string.
thank you in advance
|

October 22nd, 2012, 03:52 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 27
Time spent in forums: 8 h 7 m 21 sec
Reputation Power: 0
|
|
|
#include<stdio.h>
#include<conio.h>
void main()
{
int i, b, val;
int array[4];
char string1[4];
char string2[4];
printf("\nEnter the value of Multiplicant: ");
gets(string1);
for (i=0; i< strlen(string1); i++)
{
array[i] = (int)string1[i]-'0';
}
for(i =0;i<strlen(string1);i++)
{
printf("%d",array[i]);
}
getch();
}
|

October 22nd, 2012, 05:49 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
|
|
Wow gets()
Right from my help files -
Code:
Never use gets(). Because it is impossible to tell without knowing the data in
advance how many characters gets() will read, and because gets() will
continue to store characters past the end of the buffer, it is extremely
dangerous to use. It has been used to break computer security. Use fgets()
instead.
As for converting int to strings and strings to int, I would check out the functionality of sprintf().
|

October 22nd, 2012, 02:09 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by Cprogrmmer = Integer.ParseInt(string1[i]); |
That's not C, but rather it's C#. Though the rest of the code is not C# but rather C. That mixing of two different languages only makes your request even more confusing than it already is.
What are you trying to do?
Quote: | Originally Posted by Cprogrmmer
I don't know how to write a code for converting int array into string. |
Simple, use sprintf. And if you're going to try to do it with a for-loop, then consider sprintf plus strcat.
Though of course if we could figure out what you're trying to do, we could offer better suggestions.
And also, use code tags! Eg:
Code:
void readArr(int MD[], int MQ[])
{
int i, b, val;
int array[4];
char string1[4];
char string2[4];
printf("\nEnter the value of Multiplicant: ");
gets(string1);
/* string1 is "0011" and I need it to convert as
array[4]={0,0,1,1} */
for (int i=0; i< string1.length; i++)
{
array[i] = Integer.ParseInt(string1[i]);
}
}
There is great value in enabling us to read your code.
Last edited by dwise1_aol : October 22nd, 2012 at 02:12 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|