Solved

pass pointer to char and should return pointer char of corresponding ASCII values

Posted on 2004-03-21
9
195 Views
Last Modified: 2010-04-15
Hi all,
   Its my first quesion, which was asked for me in interview.
write function which talks pointer to char and returns pointer to char of ASCII values of corresponding char. For example, if i pass string "abcde" to function , it should return pointer to string "6162636465", (that is a=65,b=62,.....). Plz help me,
0
Comment
Question by:maheshptl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10647334
>For example, if i pass string "abcde" to function , it should return pointer to string "6162636465", (that is >a=65,b=62,.....).

for "abcde",O/P should be "6566676869"

That's pretty simple.
cast each character into an integer,use itoa() to make it into a string and use strcat() to append it to ur final string.
Then return the address of this char array(string).
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10647342
Do u actually want code for this,or u just looking for an explanation to the answer.

This is quite simple and i am actually surprised that this could be asked at an interview,
May i dare ask,which company??
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10647354
BTW,

"ABCDE" should return "6566676869"

"abcde" would return "979899100101"

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10647384
Here is the code:

#include<string.h>
char * func(char*);
int main()
{
 char *ptr="ABCDE";
 char *p;
 p=func(ptr);
 printf("%s",p);
}
char * func(char *s)
{
 char str[100];//declare to any large size
 char temp[5];//enough for string version for ASCII of 1 character
 int i;
 strcpy(str,"");
 while(*s!='\0')
 {
  i=*s;
  itoa(i,temp,10);
  strcat(str,temp);
  s++;
 }
 return str;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 10647512
char * func(char *s){
    char *str=malloc(2*strlen(s)+1);
    int i;
    for( i=0;s[i]; i+=1 ){
        sprintf(&str[2*i],"%02x",s[i]);  
    }
    return str;
}
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 40 total points
ID: 10647771
Hi ozo,
> sprintf(&str[2*i],"%02x",s[i]);  
Should be decimal - then you'd also have the issue of 2 or 3 chars:

char * func(char *s){
    char *str=malloc(3*strlen(s)+1);
    int i;
    str[0]='\0';
    for( i=0;s[i]; i+=1 ){
        sprintf(&str[strlen(str)],"%d",s[i]);  
    }
    return str;
}


Cheers,

Stefan
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10673069

These "interview questions" always leave me scratching my head....  They don't provide a operational purpose and I'm not convinced of their "evalutation" value.

Taking the above mentioned examples another step:

char * func(char *s)
{
    char *ptr;
    char *str=malloc(3*strlen(s)+1);

    ptr = str;
    while (*s)
    {
      sprintf (ptr, %d,*(s++));
      ptr+=2;
    }
    *ptr = 0;
    return str;
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question