943,935 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 16799
  • C RSS
Sep 20th, 2006
1

converting int to char in C

Expand Post »
Hi,

I am trying to convert a string length to a char and store it in an array. For some reason, even though its not giving me any error, when I try to print the array out, the place where I store the strlen is blank. Here's my code:

  1. int main () {
  2. char host[] = "<a rel="nofollow" href="http://www.google.com" target="_blank">www.google.com</a>";
  3. char delim[]=".";
  4. int i=0, index=0;
  5. char *token;
  6. char buf[100];
  7. unsigned char len_char;
  8. int length;
  9.  
  10. token = strtok(host, delim);
  11. while (token != NULL) {
  12. length = strlen(token);
  13. len_char = (unsigned char) length;
  14. printf("%c", len_char); //<----this does not print anything too!!
  15. buf[index++] = length; //add the length as the beginning of a string
  16. //printf("here%c", buf[index++]);
  17. for(i=0; i<length; i++) {
  18. buf[index++] = token[i];
  19. //printf("%c", buf[index++]);
  20. }
  21. token = strtok(NULL, delim);
  22. }
  23. printf("%d, %s\n", strlen(buf), buf);
  24. return 0;
  25. }

The output I get is as follows:

15, wwwgooglecom

where as I am expecting something like:
15, 3www6google3com

Please note that the strlen(buf) gives the expected result. It's just that the numbers are not printed. Any help would be appreciated. Thanks.

-lilpro
Similar Threads
Reputation Points: 18
Solved Threads: 0
Newbie Poster
lilprogrammer is offline Offline
2 posts
since Sep 2006
Sep 20th, 2006
0

Re: converting int to char in C

you can't just convert the int by typcasting because (1) an int is a non-printable value and (2) the value of the integer may not be in the range '0' to '9' (single digit value). The safest way is to use sprintf() to format the string with an integer. I would suggest creating another temp char buffer to format the integer.
  1. char tmpbuf[40];
  2. sprintf(tmpbuf,"%d", length);
  3. // now copy tmpbuf into buffer just like you
  4. // do the other strings

Of course, if you absolutely know the integer is a single digit, then just add '0' to it.
  1. len_char = (unsigned char) (length + '0');
Last edited by Ancient Dragon; Sep 20th, 2006 at 8:12 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 20th, 2006
3

Re: converting int to char in C

>printf("%c", len_char); //<----this does not print anything too!!
What did you expect it to print? You realize that the %c format modifier takes the integer value you supply and prints the character representation of that integer, right? In other words, it's not going to print '3'...ever. As long as the lengths are single digits, you can say len_char + '0' and get the correct representation, but once the length exceeds 9, you're SOL with this method.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 21st, 2006
1

Re: converting int to char in C

Thanks a lot. That helped. Its been a long time since I programmed in C and my concepts are a bit rusty now...but I am learning it again

-lilpro
Reputation Points: 18
Solved Threads: 0
Newbie Poster
lilprogrammer is offline Offline
2 posts
since Sep 2006
Sep 21st, 2006
1

Re: converting int to char in C

You can refer the following code if you would like to....


  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. int main () {
  7. char host[] = "<a rel="nofollow" href="http://www.google.com" target="_blank">www.google.com</a>";
  8. char delim[]=".";
  9. int i=0, index=0;
  10. char *token;
  11. char buf[100];
  12. char len_char;
  13. int length;
  14. token = strtok(host, delim);
  15. buf[0]='\0';
  16. while (token != NULL) {
  17. length = strlen(token);
  18. sprintf(buf+strlen(buf), "%d", length);
  19. sprintf(buf+strlen(buf), "%s", token);
  20. token = strtok(NULL, delim);
  21. }
  22. printf("%d, %s\n", strlen(buf), buf);
  23. return 0;
  24. }
Reputation Points: 51
Solved Threads: 1
Light Poster
Iqbal_h_a is offline Offline
30 posts
since Aug 2006
Sep 21st, 2006
0

Re: converting int to char in C

Click to Expand / Collapse  Quote originally posted by Iqbal_h_a ...
You can refer the following code if you would like to....
For what you are using #include<stdlib.h>? You dont need that.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Converting a String into an integer array
Next Thread in C Forum Timeline: Regarding getpid function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC