How to conversion array to one string...array of ints int a single string
Page 1 of 1
7 Replies - 1716 Views - Last Post: 01 July 2007 - 08:35 AM
#1
How to conversion array to one string...
Posted 10 June 2007 - 01:41 AM
imamkomc@gmail.com
Help me,
I have problem about method to convert array (type int) to one string (type char),
e.g. input as :
array[1]=1
array[2]=2
array[3]=3
array[4]=4
.
.
.
array[n]=n
stuff of every array are independent.
If i want in the output denote an one string as below :
I am defined variabel name of string is *string than the product is string="1234.....n".
How to implementation solve the problem in the C++ Program.
Thanks.
ImamkomC++
Replies To: How to conversion array to one string...
#2
Re: How to conversion array to one string...
Posted 10 June 2007 - 02:56 AM
imamkomc, on 10 Jun, 2007 - 01:41 AM, said:
imamkomc@gmail.com
Help me,
I have problem about method to convert array (type int) to one string (type char),
e.g. input as :
array[1]=1
array[2]=2
array[3]=3
array[4]=4
.
.
.
array[n]=n
stuff of every array are independent.
If i want in the output denote an one string as below :
I am defined variabel name of string is *string than the product is string="1234.....n".
How to implementation solve the problem in the C++ Program.
Thanks.
ImamkomC++
hi
Conversion falls into two categories:
- Implicit
- Explicit
int a = 5; char b = a;
Explicit occurs when the programmer uses one of three mechanisms in code that can force conversion. These are
referred to as typecasting, and there are three that I can think of:
- (new_type) old_type
- static_cast < new_type > old_type
- dynamic_cast < new_type > old_type
Each one has uses throughout code, it is preferred to use static_cast in lieu of (new_type) old_type. dynamic_cast is generally used in the OOP arena to enable casting up and down the inheritance tree.
Hope this helps.
#3
Re: How to conversion array to one string...
Posted 10 June 2007 - 10:31 AM
You know Gregory I like to read you occasional posts because I learn a lot. Not really the broad topics but the little things that I have either overlooked or forgotten, or never really understood. I myself have never really known what the difference or reason behind having a static_cast and a dynamic_cast. I just use the standard C cast unless (for example windows programming) it just seems to be what everyone else is doing (i.e. I copied code from somewhere and never changed that line because I didn't really know what it did.)
Though I am nor sure that discussion really helps change an array of integers into a string... Does the string class have a native conversion from integers to strings?
I would think that a loop using a stringstream or an sptrintf() would be the way to go.
Though I am nor sure that discussion really helps change an array of integers into a string... Does the string class have a native conversion from integers to strings?
I would think that a loop using a stringstream or an sptrintf() would be the way to go.
#4
Re: How to conversion array to one string...
Posted 12 June 2007 - 01:46 AM
gregoryH, on 10 Jun, 2007 - 02:56 AM, said:
imamkomc, on 10 Jun, 2007 - 01:41 AM, said:
imamkomc@gmail.com
Help me,
I have problem about method to convert array (type int) to one string (type char),
e.g. input as :
array[1]=1
array[2]=2
array[3]=3
array[4]=4
.
.
.
array[n]=n
stuff of every array are independent.
If i want in the output denote an one string as below :
I am defined variabel name of string is *string than the product is string="1234.....n".
How to implementation solve the problem in the C++ Program.
Thanks.
ImamkomC++
hi
Conversion falls into two categories:
- Implicit
- Explicit
int a = 5; char b = a;
Explicit occurs when the programmer uses one of three mechanisms in code that can force conversion. These are
referred to as typecasting, and there are three that I can think of:
- (new_type) old_type
- static_cast < new_type > old_type
- dynamic_cast < new_type > old_type
Hope this helps.
Hi,
I tried to use static_cast for conversion but while displaying using cout it prints garbage value.
I tried to convert it using itoa() function...
Is it the correct way.. please tell
# include<iostream.h>
# include<stdlib.h>
# include<stdio.h>
int main()
{
int iarr[4],i;
char sarr[8][8];
for (i=0;i<3;i++)
{
cin>>iarr[i];
}
for (i=0;i<3;i++)
{
_itoa(iarr[i],reinterpret_cast<char *>(sarr[i]),10);
}
for (i=0;i<3;i++)
{
cout<<reinterpret_cast<char *>(sarr[i]);
}
//i= sprintf( buffer, " String: %s\n", istr[0] );
//printf( "Output:\n%s\ncharacter count = %d\n", buffer, i );
//j= sprintf( buffer, " String: %s\n", istr[1] );
//printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
return 0;
}
EDIT : Code Tags Added
MOD COMMENT : Enclose Source Code within the [ code ] and [ /code ] block.
#5
Re: How to conversion array to one string...
Posted 12 June 2007 - 02:59 AM
For smaller values of n, you can use a single loop and extract the number into an unsigned long variable, and then use the itoa() function.
Eg. With reference to your example:
1234 = (4 * 10^0) + (3 * 10^1) + (2 * 10^2) + (1 * 10^3)
^ = Power Sign.
This will work only for numbers that fall below the unsigned long range.
Eg. With reference to your example:
1234 = (4 * 10^0) + (3 * 10^1) + (2 * 10^2) + (1 * 10^3)
^ = Power Sign.
This will work only for numbers that fall below the unsigned long range.
#6
Re: How to conversion array to one string...
Posted 12 June 2007 - 06:53 AM
You can convert numbers to strings using the sprintf() function.
sprintf() does not automatically expand the string, so make sure you allocate enough space.
char *str; int a = 5; sprintf(str, "%d", a);
sprintf() does not automatically expand the string, so make sure you allocate enough space.
#7
Re: How to conversion array to one string...
Posted 22 June 2007 - 02:03 AM
Hi all,
I try to make a program, but output is bad.
this my program (convert few arrays to single string) :
In order "for(i=1;i<=10;i++){", if i change value 10 become 100 or greater (>100), than output of program is bad.
help me.
Thanks.
I try to make a program, but output is bad.
this my program (convert few arrays to single string) :
#include <stdio>
#include <fstream>
#include <stdlib>
#include <conio>
#include <iomanip>
#define bit 1000 //can be change
void main()
{
int i,*arr,length,j,*split_str;
arr=new int[bit];
split_str=new int[bit];
char *str="",temp[1]; // init str_init
for(i=1;i<=10;i++){
arr[i]=random(10);
cout<<"arr["<<i<<"]="<<arr[i]<<"\n";
//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
}
cout<<"\n\n";
for(j=1;j<=10;j++){
itoa(arr[j],temp,10);
//cout<<"\ntemp ="<<temp<<"\n";
str=strcat(str,temp);
//cout<<"\nstr["<<j<<"]="<<str<<"\n";
}
cout<<"\nresult = "<<str<<"\n";
//order of strlen(str)
length=strlen(str);
cout<<"\nlength digit ="<<length<<"\n";
//spit string
cout<<"\n";
for(i=1;i<=length;i++){
split_str[i]=str[i-1]-48;
cout<<"split_str["<<i<<"]="<<split_str[i]<<"\n";
//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
}
getch();
}
In order "for(i=1;i<=10;i++){", if i change value 10 become 100 or greater (>100), than output of program is bad.
help me.
Thanks.
#8
Re: How to conversion array to one string...
Posted 01 July 2007 - 08:35 AM
Thank to all which many help me,
So, i can make whole program to be good.
Regard,
ImamKomC++
So, i can make whole program to be good.
#include <stdio>
#include <fstream>
#include <stdlib>
#include <conio>
#include <iomanip>
#define bit 1000
void main()
{
int i,*arr,length,j,*split_str;
arr=new int[bit];
split_str=new int[bit];
char *str,*temp;
str=new char[bit];
temp=new char[1];
for(i=1;i<=100;i++){
arr[i]=random(10);
cout<<"arr["<<i<<"]="<<arr[i]<<"\n";
//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
}
cout<<"\n\n";
str=strcpy(str,"");// str initial must be value NULL
for(j=1;j<=100;j++){
itoa(arr[j],temp,10); //10 explain type number (biner(2),decimal(10),.)
//cout<<"\ntemp ="<<temp<<"\n";
str=strcat(str,temp);
//cout<<"\nstr["<<j<<"]="<<str<<"\n";
}
cout<<"\nresult = "<<str<<"\n";
getch ();
//order strlen(str)
length=strlen(str);
cout<<"\nlength number ="<<length<<"\n";
//spit string
cout<<"\n";
cout<<"\nsplit again :\n";
for(i=1;i<=length;i++){
split_str[i]=str[i-1]-48;
cout<<"split_str["<<i<<"]="<<split_str[i]<<"\n";
//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
}
getch();
delete [] arr;
delete [] split_str;
delete [] str;
}
Regard,
ImamKomC++
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|