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:

int main () {
char host[] = "www.google.com";
char delim[]=".";
int i=0, index=0;
char *token;
char buf[100];
unsigned char len_char;
int length;
 
token =  strtok(host, delim);
while (token != NULL) {
 length = strlen(token);
 len_char = (unsigned char) length;
 printf("%c", len_char);    //<----this does not print anything too!!
 buf[index++] = length; //add the length as the beginning of a string
 //printf("here%c", buf[index++]);
 for(i=0; i<length; i++) {
  buf[index++] = token[i];
  //printf("%c", buf[index++]);
 }
 token = strtok(NULL, delim); 
}
printf("%d, %s\n", strlen(buf), buf);
return 0;
}

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

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.

char tmpbuf[40];
sprintf(tmpbuf,"%d", length);
// now copy tmpbuf into buffer just like you
// do the other strings

Of course, if you absolutely know the integer is a single digit, then just add '0' to it.

len_char = (unsigned char) (length + '0');

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

Comments
welcome back Miss Narue [~s.o.s~]
So here comes back the great Narue- [Grunt]
お帰りなさい。

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

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
int main () {
        char host[] = "www.google.com";
        char delim[]=".";
        int i=0, index=0;
        char *token;
        char buf[100];
        char len_char;
        int length;
        token =  strtok(host, delim);
        buf[0]='\0';
        while (token != NULL) {
                length = strlen(token);
                sprintf(buf+strlen(buf), "%d", length);
                sprintf(buf+strlen(buf), "%s", token);
                token = strtok(NULL, delim);
                }
        printf("%d, %s\n", strlen(buf), buf);
        return 0;
        }

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

For what you are using #include<stdlib.h> ? You dont need that.

This question has already been answered. Start a new discussion instead.