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

int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too

1 Answers  


Is the following code legal? struct a { int x; struct a *b; }

2 Answers  


pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"

2 Answers  


to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD

6 Answers   Synergy,


void main () { int x = 10; printf ("x = %d, y = %d", x,--x++); } a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above

2 Answers   HCL,






how to delete an element in an array

2 Answers   IBM,


main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }

1 Answers   DCE,


Cluster head selection in Wireless Sensor Network using C programming language.

0 Answers  


main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(ā€œ%dā€ ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(ā€œ%d ā€ ,*p); p++; } }

1 Answers  


Given n nodes. Find the number of different structural binary trees that can be formed using the nodes.

16 Answers   Aricent, Cisco, Directi, Qualcomm,


Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.

2 Answers  


What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }

1 Answers  


Categories