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.
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 |
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)
main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }
how to return a multiple value from a function?
#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }
What are the files which are automatically opened when a C file is executed?
hello sir,is there any function in C that can calculate number of digits in an int type variable,suppose:int a=123; 3 digits in a.what ll b answer?
What is the main difference between STRUCTURE and UNION?
void main() { char ch; for(ch=0;ch<=127;ch++) printf(ā%c %d \nā, ch, ch); }
main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }
main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }
#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }