Hi,
Is there any way of converting char to int or int to char? I want to put an int into a char array.
Thank you
Hi,
Is there any way of converting char to int or int to char? I want to put an int into a char array.
Thank you
>>char to int
atoi or strtol functions (stdlib.h)
>>int to char
sprintf, snprintf (stdio.h)
What sort of examples did you have in mind?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
somethine like this:
want to put an int into a char array into this specific index.
sprintf(s[i],"%c",num);
but this gives me
warning: passing arg 1 of `sprintf' makes pointer from integer without a cast
Well, in the case ofOriginally Posted by saphiroth
sprintf expects a char array, or "string", for the first argument, and you are sending it one element of the array.Code:sprintf(s[i],"%c",num);
If you want to put a int into a char array, I guess you can just force it.
That probably won't have the effect you want though.Code:carray[index] = someInt; /*or even typecast it */ carray[index] = (char) someInt
Maybe you want the ascii value of an integer? I'm pretty sure this only works for numbers 0-9, but
would store '7' in the array in the case that someInt == 7. If you want a two digit number, you're going to have to do a little bit more work.Code:carray[index] = '0' + someInt /* 0 <= someInt <= 9 */
I tried to force it but I got this when righting the result to a file. The boxes should have number in them.
a+b*c=+* ܁12565
b*g+n=*+ ܁12565
c*t+h=*+ ܁12565
a+u*m=+* ܁12565
t+w*i=+* ܁12565
Well, I said you could force the ints into the array, I also said it probably wouldn't have the effect you wanted.Originally Posted by saphiroth
Ok, so in the first line, you have integers: a, b, and c, and you want to put them and their result in a string to print to a file?
You do know that you can do this with fprintf().
Can you please explain very clearly what it is you are trying to do?
I simply want to replace the letters a,b,c,g,n...with numbers then put that results such as:
"a+b*c=1+5*6" into a file and do the same thing with the next line. Get it in the form of b*g+n and output to file "b*g+n=6*4+8".
Thank you
Something similar to this?Code:#include <stdio.h> int main(void) { char line [ 80 ]; fputs("Enter equation [a+b*c]: ", stdout); fflush(stdout); if ( fgets(line, sizeof line, stdin) != NULL ) { double a, b, c; if ( sscanf(line, "%lf+%lf*%lf", &a, &b, &c) == 3 ) { printf("%g+%g*%g=%g\n", a, b, c, a + b * c); } } return 0; } /* my output Enter equation [a+b*c]: 1+2*3 1+2*3=7 */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I think they want it a bit more complex than that. This is I suspect what they want:
Basicly, if I understand it right, they want to search through and find each letter, and turn it into the number of the place it falls in the alphabet.Code:Output: "Please enter equation: " Input: "a+w-z*a" Output: "Ok, 'a+w-z*a' = '1+24-26*1'"
This should be easy to do for them, if they just think about it.
So there they have it. That's all you have to do. Now you simply write something that looks at a letter and gives you a value 1 through 26 for it. Surely you can think of some way to do that, right? How would you do it on paper? Walk through the process you yourself would take in your own mind to find out what a letter was.Code:for each character of input if this is a letter convert to its "alphabetical value" else don't do anything to this character
Now turn that into code.
When you get stuck, post your code attempt, and we'll give you a few pointers.
Quzah.
Hope is the first step on the road to disappointment.
I want to thank all of you for your help. quzah you are correct about the program. Well almost. I do have to convert a letter into the number it falls in the alphabet and i did that by putting all the letter into an array and just incrementing the index by one so the letter b would be in the index 1 but its the second letter so I increment the index by one and get
b = 2. What I have to do is: Create 2 forks. in file 1 i have the equations such as a+b*y that the parent process reads and then process them by exchanging the right number corresponding to that letter then it place this answer with its pid into file 2. The child process gets the file 2 and adds the stuff up and places its pid next to it and puts the answer into file 3. Then the grandchild process reads file 3 and prints out the answer to the screen. I wrote out most of the code but im having troubles cause Im not too familiar with C syntax. Since the 3 processes execute independently BUT file 2 has to be filled up so the child can process all the equations in it and grandchild can print all the answers, i am trying to find a way(without shared memory) so that the child will keep looping or something until the parent outputs the next equation to file 2 and so on. My other problem is putting the formatted(number into letter) equation into a char array since the output has to have '*' and '+'. Ill include the code if someone needs to look at it. Its not finished though.
Code:#include <stdio.h> FILE *Rfp,*Wfp; char s[4]; char letter[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; char c; int main() { void GrandchildProc(void); void ParentProc(void); void ChildProc(void); int compareInsert(char *s[4],int nums[2]); if(fork()==0){ if(fork()==0){ //Grandchild //GrandchildProc(); } else{ //Child ChildProc(); } } //------------------------------------------------------------------- else{ //Parent //ParentProc(); } return 0; } //Functions---------------------------------------------------------- void ParentProc(){ int x,i,num,k; int nums[3]; char final[16+sizeof(getpid())]; Rfp = fopen("File1.txt","r"); Wfp = fopen("File2.txt","w"); if(!Rfp)exit(1); else{ while(fscanf(Rfp, "%s",s)!=EOF){ for(i=0;i<=sizeof(s);i++){ final[i] = s[i]; }//for k = 0; for(i=0;i <= sizeof(s);i++){ for(x=0;x <= sizeof(letter);x++){ if(letter[x] == s[i]){ num = x+1; nums[k] = num; //printf("%c%d%d\n",s[i],i,num); k++; } //if }//for }//for compareInsert(s,nums); final[5] = '='; for(i=0,x=6;i<=sizeof(s);i++,x++){ final[x] = s[i]; }//for for(i=11;i<=15;i++){ final[i] = ' '; }//for //printf("%d",sizeof(final)); fprintf(Wfp,"%s%d%c",final,getpid(),'\n'); } //while fclose(Rfp); fclose(Wfp); }//else }//parent int compareInsert(char *s[4],int nums[3]){ int num1,num2,num3; num1 = nums[0]; num2 = nums[1]; num3 = nums[2]; if(*s[1] == '*' && *s[3] == '*'){ sprintf(*s,"%d*%d*%d",num1,num2,num3); }//if else if(*s[1] == '+' && *s[3] == '+'){ sprintf(*s,"%d+%d+%d",num1,num2,num3); }//else if else if(*s[1] == '+' && *s[3] == '*'){ sprintf(*s,"%d+%d*%d",num1,num2,num3); }//else if else if(*s[1] == '*' && *s[3] == '+'){ sprintf(*s,"%d*%d+%d",num1,num2,num3); }//else if }//compareInsert void ChildProc(){ int start, end, i, j; int startTF,endTF = 0; char read[21]; char newRead[5]; Rfp = fopen("File2.txt","r"); Wfp = fopen("Fiel3.txt","w"); if(!Rfp)exit(1); else{ while(fscanf(Rfp, "%s",read)!=EOF){ printf("%s%c",read,'\n'); for(i=0;i<=sizeof(read);i++){ if(read[i] == '='){ start = i; startTF = 1; //printf("start: %d%c",start,'\n'); }//if if(read[i] == ' '){ end = i; endTF = 1; //printf("end: %d%c",end,'\n'); }//if if(startTF == 1 && endTF == 1){ break; } }//for j = 0; for(start;start<=end;start++){ newRead[j]=read[start]; j++; }//for //printf("%s%c",newRead,'\n'); }//while fclose(Rfp); fclose(Wfp); }//else }//Child void GrandchildProc(){ Rfp = fopen("File3.txt","r"); if(!Rfp)exit(1); else{ while(strcmp((fgets(s,20,Rfp)),"END")!=0){ printf("Grandchild: %s\n",s); } fclose(Rfp); } }//Grandchild