Write a C program to print look and say sequence?
For example if u get the input as 1 then the sequence is
11 21 1211 111221 312211 12112221 .......(it counts the no. of 1s,2s etc which is in successive order) and this sequence is used in run-length encoding.



Write a C program to print look and say sequence? For example if u get the input as 1 then the sequ..

Answer / chuck dorris

#include <string>
#include <sstream>

std::string lookandsay(const std::string &s)
{
std::ostringstream r;

for (unsigned int i = 0; i != s.length(); ) {
unsigned int new_i = s.find_first_not_of(s[i], i+1);
if (new_i == std::string::npos)
new_i = s.length();

r << new_i - i << s[i];
i = new_i;
}
return r.str();
}

#include <iostream>

int main()
{
std::string laf = "1";

std::cout << laf << std::endl;
for (int i = 0; i < 10; i++) {
laf = lookandsay(laf);
std::cout << laf << std::endl;
}

return 0;
}

Is This Answer Correct ?    0 Yes 8 No

Post New Answer

More C Code Interview Questions

main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”

5 Answers   HCL,


void main() { char ch; for(ch=0;ch<=127;ch++) printf(“%c %d \n“, ch, ch); }

1 Answers  


main() { char *p = “ayqm”; printf(“%c”,++*(p++)); }

29 Answers   IBM, TCS, UGC NET, Wipro,


Find the largest number in a binary tree

7 Answers   Infosys,


main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }

1 Answers  






main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); }

1 Answers  


write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?

1 Answers  


main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }

1 Answers  


write a program for area of circumference of shapes

0 Answers  


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above

4 Answers   Corporate Society, HCL,


Write a program to print a square of size 5 by using the character S.

6 Answers   Microsoft,


How do you create a really large matrix (i.e. 3500x3500) in C without having the program crash? I can only reach up to 2500. It must have something to do with lack of memory. Please help!

1 Answers  


Categories