Wednesday, 10 December 2014

Programming Quiz-5

Q: What is the output of this C code?

#include <stdio.h>

void main()

{

    static int x;

    if (x++ < 2)

    main();

}

 Options:

a) Infinite calls to main
b) Run time error
c) Varies
d) main is called twice

Answer: D

Programming Quiz-4

Q: What is the output of this C code?

#include <stdio.h>

struct student

{

    char a[5];

};

void main()

{

    struct student s[] = {"hi", "hey"};

    printf("%c", s[0].a[1]);

}

 Options:

a) h
b) i
c) e
d) y

Answer: B

Programming Quiz-3

Q: What is the output of the below c code?

#include <stdio.h>

void main()

{

    char *s = "hello";

    char *p = s;

    printf("%p\t%p", p, s);

}

Options:

a) Different address is printed
b) Same address is printed
c) Run time error
d) Nothing

Answer: B

Programming Quiz-2

Q: What is the output of this C code?

#include <stdio.h>

void main()

{

    int x = 97;

    int y = sizeof(x++);

    printf("x is %d", x);

}

Options:

a) x is 97
b) x is 98
c) x is 99
d) Run time error

Answer:  A

Programming Quiz-1

Q: Write a program in which print 0 if 1 is provided as input and print 1 if 0 is provided.
Condition apply :
(i.) you can't use predefined function(like logical not).
(ii.) You can't use if-else condition.

Please Do comments...

Thursday, 4 December 2014

List of valid escape sequence

To learn escape sequence you can use following sentence for easy learning

Very Big Farms Need A Red Tractor

So valid escape sequence are:

\v for Vertical Tab
\b for Backspace
\f for Form Feed
\n for Newline(Line Feed)
\a for Alarm(Beep, Bell)
\r for Carriage Return
\t for Horizontal Return


Other escape sequence are:

\\ for Backslash
\' for Single Quotation Mark
\" for Double Quotation Mark
\? for Question Mark

Wednesday, 3 December 2014

How to read string of any size in C++?

#include<iostream.h>
#include<stdio.h>
#define SIZE 1
void main( )
{
             char* str[SIZE];
             gets(str[0]);         //Read string of any string
             cout<<str[0];      
 }

Function to find whether a given string is a substring of another string

Following function finds whether a given string is a substring of another string. If a given string is a substring of another string it returns 1 which means success, otherwise it will return 0.

int substr(char* str1, char* str2)
{
                  int m, n, i, j;
                  m=n=0;

                  //Find the lengths of strings
                  while(str1[m])
                             ++m;
                  while(str2[n])
                              ++n;

                   for(i=0; i<m-n+1;++i)
                   {
                              for(j=0; j<n;++j)
                                         if(str2[j] != str1[i+j])
                                                       break;
                                 
                               if( j == n )
                                         return 1;        //Substring exists
                     }
           
                      return 0;         //Substring does not exists
}