C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 22nd, 2012, 12:46 AM
Cprogrmmer Cprogrmmer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 1 Cprogrmmer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old October 22nd, 2012, 03:52 AM
Technovicez Technovicez is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 27 Technovicez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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();




}

Reply With Quote
  #3  
Old October 22nd, 2012, 05:49 AM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
Wow gets()

Code:
gets(string1);


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().

Reply With Quote
  #4  
Old October 22nd, 2012, 02:09 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 14 h 57 m 21 sec
Reputation Power: 1949
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > String to int array conversion in C programming

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap