| | |
Function to convert integer to single char.
Thread Solved
![]() |
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
Hello, I'm new to C and having this problem. What I need to do is add a single integer (0-9) to an (obviously char) string. More specifically, I want to add the value of a function that returns an integer (0-9) to a string. I don't want to change the integer value in any way, just to convert it to a form in which I could add it to a string.
I have checked out the itoa() function, but apparently it only stores the converted integer into a whole new string, not a single subscript/element of a string as I need.
Also, the function should return as a value the integer in the char form that it needs to be in order for it to be stored in the string.
"Pseudocode" example: TheString[i]=ConvertToInt(FunctionThatReturnsInt());
Does a function like this exist in C? (If not, I guess I will have to code it, but I'd hate to "re-invent the wheel", so I am asking
) Thanks.
I have checked out the itoa() function, but apparently it only stores the converted integer into a whole new string, not a single subscript/element of a string as I need.
Also, the function should return as a value the integer in the char form that it needs to be in order for it to be stored in the string.
"Pseudocode" example: TheString[i]=ConvertToInt(FunctionThatReturnsInt());
Does a function like this exist in C? (If not, I guess I will have to code it, but I'd hate to "re-invent the wheel", so I am asking
) Thanks. I'm not a C guy by any means, but I think you can cast it.... such as:
Someone who Codes a lot of C, please check this over...
C Syntax (Toggle Plain Text)
newvar = (int)x + (int)y;
Someone who Codes a lot of C, please check this over...
For a value 0-9, you can just add '0' to get the corresponding digit character.
#include <stdio.h>
int main(void)
{
char text[] = "StringX";
int digit;
for (digit = 0; digit < 10; ++digit)
{
text[6] = digit + '0';
puts(text);
}
return 0;
}
/* my output
String0
String1
String2
String3
String4
String5
String6
String7
String8
String9
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 1
man its easy u have to use ascii code i will show u how with a simple program ok i hope this could help you and pls send me a reply telling me if thats what ur looking for :this silly software works o 0 to 9;
#include<iostream.h>
int main ()
{
int a;
cout<<"enter nnumber";
cin>>a;
char x;
x=a+48;
cout<<x<<endl;
return 0;
}
#include<iostream.h>
int main ()
{
int a;
cout<<"enter nnumber";
cin>>a;
char x;
x=a+48;
cout<<x<<endl;
return 0;
}
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 1
if ur looking for a function i will post it, it is also 1 interger from 0 to 9 i will do later a small program for larger integers:
c Syntax (Toggle Plain Text)
#include<iostream.h> char integer_to_char(int);//prototype int main ()//function main starts { int a;//varibale that will hold the integer value cout<<"enter nnumber";//msg that prompt the user cin>>a;//user input cout<<"the number represented in char form is:"<<integer_to_char(a)<<endl;//calling the function return 0; } //function program char integer_to_char(int x) { char y; y=x+48; return y; }
Last edited by WolfPack; Jan 21st, 2008 at 11:50 am. Reason: added [code=c][/code] tags. Next time use them when posting code.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: string array substitution
- Next Thread: Function to release CPU?
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file floatingpointvalidation fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram inches input intmain() iso kernel keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf opendocumentformat openwebfoundation owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault sequential single socket socketprograming socketprogramming stack standard string systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






