Microsoft Interview Question


Country: India




Comment hidden because of low score. Click to expand.
6
of 6 vote

void fun(char str[])
{
     int len=strlen(str);
     int i,j;
     for(i=len-1,j=2*len-1;i>=0;i--,j-=2)
     {
                str[j]=((int)str[i])%10 + '0';
                str[j-1]=((int)str[i])/10 + '0';
                }
     str[2*len]='\0';
     }

- Dheeraj Sharma March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks.....correct approach.

- Anonymous March 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is not correct. For each char in string you need to have two bytes in array. So "j=2*len-1" is not correct. It should be "j=3*len-1". Please let me know if I am wrong.

- Dumbo March 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh ! I am sorry, I got it. At end we will not have string but ASCII value only.

- Dumbo March 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

How can u do it in place??? The new string is longer.

- Anonymous March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

void print_ascii(char str[] ){
int len = strlen(str);

int last_index = len - 1;

int i =0,j,count = 0;
int val;
while(count < len){

for ( j = last_index ; j >= i ; j -- )
str[j+1] = str[j];


val = str[i];
str[i] = '0'+(val/10);
str[i+1] = '0'+(val%10);



last_index++;

i = i + 2;
count++;

}
str[last_index + 1 ] = '\0';

printf("\n ans str = %s",str);
}

- Anonymous March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
char c[100];
int i=0;
printf("Enter the string :");
scanf("%s",c);
while(c[i]!='\0')
{

printf("%d",c[i]);
i++;
}
getch();
return 0;
}

- Anonymous March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void str_to_ascii(char *a)
{
  if(!a)
    return;
  else
  {
    int orig_len=strlen(a);
    int new_len=orig_len*2;
    int i,j,value;
    for(i=orig_len-1, j=new_len-1; i>=0; i--, j--)
    {
      value=a[i];
      a[j]=(int)'0'+(value%10);
      j--;
      value=value/10;
      a[j]=(int)'0' + value;
    }
    a[new_len]='\0';
}

- Anonymous March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is assuming that the original string(array) has enough spaces at its end to accomodate the double digits of ascii values and also that the string is in uppercase characters only.

- Anonymous March 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you assume that all characters will be caps then your solution will work else you shall have to consider 3 chars too in case of small characters that too... It is not a big problem but just wanted to point out.

- abhishekatuw March 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since we're working with "in-place", I'm assuming we're going to pass in something that's more edit-friendly (I'm using Java). So I'm using StringBuilder:

public class ToAscii
{
	public static void toAscii(StringBuilder sb)
	{
		int length = sb.length();
		int numberOffset = 0;
		for(int i = 0; i < length; i++)
		{
			String numberString = new String("" + (int)sb.charAt(numberOffset));
			sb.insert(numberOffset, numberString);
			numberOffset += numberString.length();
			sb.deleteCharAt(numberOffset);
		}
		System.out.println(sb.toString());
	}
}

- movingincircles March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A quick and dirty solution

#include <stdio.h>
void main()
{
stringtoascii();
}

void stringtoascii()
{

char *array="HELLOWORLD";

while(*array !='\0')
{
printf("The value of the string is %d\n",*array);
*array++;
}
}

- VivekChauhan March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//this is working fine
void print_ascii(char str[] ){
int len = strlen(str);
str=(char *)realloc(str,2*len*(sizeof(char)));
int i=0;
int j;

str[2*len]='\0';
for(j=(2*len)-1;j>=1;){
i=(int)j/2;
str[j] = (char)(48+(str[i]%10));
str[j-1] = (char)(48+(str[i]/10));
j=j-2;
}
printf("%s\n",str);
}

- sameer March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that input is only in uppercase...

void replaceCharsByASCII(std::string &input){

	int length = input.length() * 2;
	for(int i = 0; i < length; i+=2){
		int a = input[i];
		std::stringstream ss;
		ss << a;
		std::string g = ss.str();
		input.replace(i,1,g);
	}
	
	
}

- Abdul Samad April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringToAscii {

	public static void main(String[] args) {
		
		char str[] = "HELLOWORLD".toCharArray();

		
		StringBuilder sb = new StringBuilder();

		for (int i=0 ; i<str.length ; i++){

			sb.append((int)str[i]);

		}

		System.out.println(" Final String " + sb.toString());
	}
}

- sid_tintin May 09, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More