Saturday, January 08, 2011

User-Define Function...exercise solution of C,C++

                   USER-DEFINE FUNCTION
Review Questions 
                               
Question: State whether the following statements are true or false.

         

      (a) C function can return only one value under their function name.
                Answer: False

                  (b) A function in C should have at least one argument.

          Answer: True
   (c) A function can be defined and placed before the main function.

       Answer: False
                 (d) A function can be defined within the main function.
                      Answer: False.

         (e) An user-defined function must be called at least once; otherwise
                      a warming message will be issue

                       Answer: True.
                 (f) Any name can be used as a function name.
                      Answer: False.

. (g) Only a void type function can have void as its argument
                           
                 Answer: False.

              (h) When variable values are passed to function, a copy of them are                                                                         
      created in the memory.

     Answer: True.

     (i) Program execution always begins in the main function irrespective of                           location in the program.
      Answer: True.
    (j)  Global variable are visible in all blocks and function in the   program
       Answer: False.
.
    (k)  A function can call itself.
       Answer: True
(l )   A function without a return statement is illegal.
      Answer: False.
(m)     Global variable can not be declared as auto variables.
         Answer: False.
(n)    A function prototype must always be placed outside the calling function.
         Answer: True
(o)     The return type of a function int by default.
                         Answer: True.

(p)     The variable names used in prototype should match those used in the function definition.
     Answer: True.

(q)     In parameter passing by pointers, the formal parameters must be prefixed with the symbol * in their declarations.
     Answer: True.

(r)     In parameter passing by pointers, the actual parameters in the function call may be variables or constants.
     Answer: False.

(s)      In passing arrays to function, the function call must have the name of the array to be passed without brackets.
        Answer: False.
(t)      In passing strings to function, the actual parameter must be name of the strings post-fixed with size in brackets.
      Answer: True.
Question: Fill in the blanks in the following statements.

(a)    The parameters used in a function call are called …………….
Answer: actual parameter.
(b)   A variable declared inside a function is called ……….. variable.
Answer: local
(c)    By default………….is the return type of a function.
Answer: int
(d)   In passing by pointers ,the variable of the formal parameters
must be prefixed with …………….operator in their declaration.
Answer: indirection
(e)    In prototype declaration specifying .parameter………... is optional.
Answer: name
(f)    …………… refers to the region  where a variable is actually variable for use.
Answer: Prototype
(g)   A function that calls itself is known as a………….. function.
Answer: recursive
(h)   If a local variable has to retain its value between calls to the function, it must be declared as ………...
Answer: void
(i)     A data ……….. aids the compiler to check the matching between the actual arguments and the formal ones .
Answer: types
(j)     A variable declared inside a function by default assumes …………storage class.
Answer: without
Question: The main is a user-defined function. How does it differ from other  user-defined function?

Answer:
 The main is an example of user-defined function. Printf  and  scanf belong to the category of library functions. We have also used other library functions such as sqrt, cos, strcat, etc. The main distinction between these two categories is that library functions are not required to be written by us whereas a user-defined function has to be developed by the user at the time of writing a program.   
Question: Describe the two ways of passing parameters to function .When do you prefer to use each of them?
Answer:
 The parameter list declars the variables that will receive the data sent by the colling program. They serve as input data to the function to carry out the specified task.Since they represent actual input valus, they are often referred to as formal parameters.These parameters can also be used to send values to the colling programs.This aspect will be covered  later when we discuss more about functions.The parameters are also known as arguments.                  
 The parameter list contains declaration  of variables separated by commas and surrounded by parentheses.
Examples;
float quadratic(int a, int b, int c) {….}

                                                            

Question: What is prototyping? Why is it necessary?

Answer:
Prototypes enable the compiler to provide stronger type checking, somewhat like that provided by languages such as pascle. When you use prototypes, the compiler can find and report any questionable the conversions between the arguments used to call a function and the type of its parameters. The compiler will also catch differences between the number of arguments used to call a function and the number of parameters in the function. A function prototype consists of four parts.
(1)Function type
(2)Function name
(3)Parameter list
(4)Terminating  semicolon.
The general form of a function prototype is function-type function-name (parameter list);
  
It is a good programming style to declare prototypes in the global declaration section before main. It adds flexibility, provides as excellent quick reference to the functions used in the program, and enhances documentation. 

Question: Distinguish between the following:

  (a)Actual and formal arguments

  Answer:
If the actual parameters are more than the formal parameters the extra actual arguments will be discarded.
On the other hand, if the actual are less than the formals, the unmatched formal arguments will be initialized to some garbage.

 (b) Global and local variables
 Answer:
A global variable used in a function will retain its value for future use. A   global variable is visible only form the point of its declaration to the end of the program.  A local variable is a variable that is defined inside a function and used without having any role in the communication between functions.
 (c) Automatic and static variables
  Answer:    
A variable declared inside a function without storage class specification default , an automatic variable. For instance, the storage class of the variable number in the example below is automatic.
                                         main (  )
                                      {
                                        int number;
                                        -------------
                                        -------------
                                      }
                   
  A static variable may be either an internal type or an external type. Depending on the place of declaration. Internal static variables are those which are declared inside a function .Therefore, internal static variables can be used to return values between function calls. For example, it can be used to count the number of calls made to a function.    

  (d) Scope and visibility variables
   Answer:
The scope of variables determine s over what region of  the program a variable is actually available for use (active) .on the other hand the visibility refers to the accessibility of a variable form the memory.  

  (e) & operator and * operator

   Answer:
The operator & is called the address operator. On the other hand,
The operator * is known as indirection operator because it gives an indirect            reference to a variable through its address. 


   
 
Question:  Explain what is likely to happen when the following situations are   encountered in a program
          .
        
(a)Actual arguments are less than the formal arguments in a function.

Answer: If the actual are less than the formals, the unmatched formal arguments will be initialized to some garbage.


(b) Data type of one of the actual arguments does not match with the type of the corresponding formal argument.

Answer: The formal and actual arguments must match exactly in type, order and number .their names, however, do not need to match.


(a)Data type of one of the argument in a prototype does not match with the type of the corresponding formal parameter in the header line.

Answer: Since the prototype is not available, c will assume that the return type is an integer and that the types of parameters match the formal definitions.
If these assumptions are wrong, the linker will fail and we will have to change the program. The moral is that we must always include prototype declarations preferably in global declaration section.    


(a)The order of actual parameters in the function call is different from the order of formal parameters in a function where all the parameters are of the same type.

Answer:
The parameters used in prototypes and function definitions are called formal parameters and those used in function calls are called actual parameters. Actual parameters used in a calling statement may be simple constants , variables  or expressions. The formal and actual parameters must match exactly in type , order and number . Their names, however do not need to match.   

(a)The type of expression used in return statement does not match with the type of the function.
Question: Which of the following prototype declarations are invalid? Why?
 (a) int (fun) void;
    Answer: valid

 (b)double fun (void)
     Answer: double fun (void);

 (c) float fun (x,y,n);
      Answer: float fun (float x, float y, float n);

 (d) void fun (void, void);
      Answer: void fun (void);

(e)int fun (int a, b);
    Answer: int fun (int a, int b);

(f)fun (int, float, char );
    Answer: valid

(g)void fun (int a, int  &b);
     Answer: void fun (int a, int b);

Question: Which of the following header lines are invalid? Why?

(a)     float average  ( float  x, float y, float z);
Answer: valid
(b)    double power ( double a,  int n-1)
Answer: double power (double a, double n-1);
(c)     int product  ( int m, 10)
Answer: int product (int m, int 10);
(d)    double minimum ( double x; double y;)
Answer: double minimum (double x, double y);
(e)     int  mul  ( int x , y)
Answer: int mul (int x, int y);
(f)     exchange ( int *a, int *b)
Answer: exchange (int *a, int *b);
(g)    void  sum ( int a, int b, int  &c)
Answer: void sum (int a, int b ,int c);
      
Question: Find errors , if any, in the following  function  definitions:
        
           ( a)  void  abc ( int a, int b)
                      {
                              int  c;        
                              .............
                              return  (c);
                        }
                  Answer: void abc (int a,int b)
                         {
                               int c;
                               -------------
                          }
             
(b)       int  abc ( int a, int b)
{
  ....................
........................
}

Answer: int abc (int a; int b)
        {
             int c;
             -----------
             return(c);
          }
         

(c)       int  abc  ( int a, int b)

{
         double c = a+b;
         return  (c);
}
                     Answer: int abc (int a, int b)
                      {
                         int c =a+b;
                          return (c);
                       }
               

(d)      void  abc  ( void)
{
   ...................
   ....................
   return;
                      }
                 

                    Answer: void abc (void)
                          {
                           int c;
                           -------------
                           }
(e)       int  abc ( void)
{
     ...........................
    ............................
    return ;
}
Answer: Error in declaration.

Question: Find errors in the following function calls:
            (a)void xyz();
Answer:
 void xyz (  )
            (b)xyx (void);
Answer:
  void xyx (void)
            (c)xyx (int x, int y);
Answer:
 int xyx (int x, int y)
            (d)xyzz ();
Answer:
int xyzz(  )
            (e)xyz ()+xyz();
Answer:
int xyz ( ) + int xyz ( )
Question: A Function to divide two flowing point numbers is as follows:

divide (float  x,    float  y)
{

     Return (x/y);
}
What will be the value of the following function calls
(a)    divide (10,2)
                     Answer: 5.000000.
(b)   divide (9,2)
                     Answer: 4.500000.
(c)    divide (4.5,1.5)
                     Answer: 3.000000.
(d)   divide (2.0,3.0)
                      Answer: .6777777.
Question: What will be the effect on the above function calls if we change the header line as follow:
(a)   int divide (int x,int y)
(b)   double divide(float x, float y)

 Question: Determine the output of the following program?
int prod (int m, int n);
main (  )

{
      int  x=10;
      int  y=20;
      int  p, q;
      P=prod (x,y);
      q=prod (p, prod (x,z));
      printf  (“%d %d\n”,p,q);
}
int prod (int a,int b)
{
        return  (a*b);
}


Answer: p=200
        q =4000
Question: What will be the output of the following program?
Void test (int *a);
main(  )
{
            int  x=50;
            test  ( &x);
            printf (“%d\n”, x);
 }
void  test  (  int  *a);
 {
             *a=*a +50;
 }


Answer: 100.
Question: The function test is coded as follows:
            int  test  ( int  number)
           {
                       int m,n=0;
                       while  (number)
                      {
                            m= number % 10;
                            if ( m% 2 )
                                 n=n +1;
                             number = number/10;
                       }
                        return ( n) ;
             }

   What will be the values of x and y when the following statement are executed
              int  x = test (135);
                ans:x=3
               
              int  y=  test  (246 );
               Answer: y=o

Question: Enumerate the rules that apply to a function call.

Answer:

A function call is a postfix expression. The operator (…) is at a very high level of precedence, therefore, when a function call is used as a part of an expression , it will be evaluated first , unless parentheses are used to change the order of precedence.
   
In a function call, the function name is the operand and the parameters set (..) which contains the actual parameters is the operator . The actual parameters must match the functions formal parameters in type, order and number. Multiple actual parameters must be separated by commas.    


Question: Summarize the rules for passing parameters to functions by Pointers.       

 Answer:
The types of the actual and formal arguments must be same.
The actual arguments must be the addresses of variables that are local to the calling function.
The formal arguments in the function header must b\e prefixed by the indirection operator.
In the prototype the arguments must be prefixed by the symbol *.
To access the value of an actual argument in the called function, we must use the corresponding formal argument prefixed with the indirection operator *.
   
Question: What are the rules that govern the passing of arrays to functions.
Answer:
The function must be called by passing only the name of the array.
In the function definition, the formal parameter must be an array type, the size of the array does not need to be specified.
The function prototype must show that the argument is an array.

Question: State the problems we are likely to encounter when we pass global variables as parameters to functions 
 Answer:
 Since all functions in a program source file can access global Variables, they can be used for passing values between the   functions. However, using   global variables as parameters for passing values poses certain problems.
       
The values of global variables which are sent to the called function may be changed in advariently by the called functions.
Functions are supposed to be independent and isolated modules. This character is lost, if they use global variables.
It is not immediately apparent to the reader which values are being sent to the called function.
A function that uses global variables suffers from reusability.


Wednesday, January 05, 2011

HAJEE MOHAMMAD DANESH SCIENCE & TECHNOLOGY UNIVERSITY,DINAJPUR.....ADMISSION DATE HAS BEEN PUBLISHED

                                                     MERIT LIST:


                                                      A-UNIT: 16-01-2011

                                                       B & C -UNIT: 17-01-2011 


                                                   WAITING LIST :


                                                    A-UNIT: 19-01-2011
                                                    B & C - UNIT: 20-01-2011



..................................................................................
...........................................................
.........................................
..........................
.............
.....
..

C,C++,JAVA PROGRAMMMING IN HAJEE MOHAMMAD DANESH SCIENCE & TECHNOLOGY UNIVERSITY


          CHAPTER-8
CHARACTER ARRAYS AND STRINGS

Review Questions:

RQ-8.1: State whether the following statements are true or false.
(a) When initializing a string variable during its declaration, we must include the null character as part of the string constant, like ‘GOOD\0’.
Answer: False.
(b) The gets function automatically appends the null character at the end of  the string read from the keyboard.
Answer: True.
(c) When reading a string with scanf, it automatically inserts the terminating null character.
Answer: True.
(d) String variables cannot be used with the assignment operator.
Answer: True.
(e) We cannot perform arithmetic operations on character variables.
Answer: True.
(f) We can assign a character constant or a character variable to an int type variable.
Answer: False.
(g) The function scanf cannot be used in any way to read a line of text with the white spaces.
Answer: True.
(h) The ASCII character set consists of 128 distinct characters.
Answer: False.
(i)  In the ASCII collating sequence, the uppercase letters precede lowercase letters.
Answer: True.
(j) In C, it is illegal to mix character data with numeric data in arithmetic operations.
Answer: False.
(k) The function getchar skips white-space during input.
Answer: False.
(l) In C, strings cannot be initialized at run time.
Answer: False.
(m) The input function gets has one string parameter.
Answer: True.
(n) The function call strcpy (s2, s1); copies string s2 into string s1.
Answer: False.
(o) The function call strcmp ( ‘abc’ , ‘ABC’ ); returns a positive number.
Answer: True.

Question-8.2 Fill in the blanks in the following statements.
(a) We can use the conversion specification ….. in scanf to read a line of text.
Answer: code.
(b) We can initialize a string using the string manipulation function ……... 
Answer:
(c) The function strncat has (three) parameters.
Answer: three
(d) To use the function atoi in a program, we must include the header file ……. .
Answer: <std.lib.h>
(e) The function ……. does not require any conversion specification to read a string from the keyboard.
Answer: gets.
(f) The function ……… is used to determine the length of a string.
Ans: strlen.
(g) The ……….string manipulation function determines if a character is contained in a string.
Answer: strstr.
(h) The function ………..is used to sort the strings in alphabetical order.
Answer: ASCII.
(i)                The function call strcat (s2,s1); appends …… to …….
Answer: One, another.
(j) The printf may be replaced by ……… function for printing trings.
Answer: Puts.

Question-8.3: Describe the limitations of  using getchar and scanf functions for reading strings.
Answer:
By using getchar we can read only one character from the keyboard.
We can read only string without white spaces by using  scanf function.

Question-8.4: Character strings in C are automatically terminated by the null character.
Explain how this feature helps in string manipulations.
Answer: We know that a string is not a data types in c, but it is consider a data structure stored in array. The string is a variable-length structure and is stored in a fixed-array. therefore, the last element of an array need not be represent at the end.
It is automatically terminate by null character.
Question-8.5: Strings can be assigned values as follows:
(a) During type declaration                         char string[]={“.........”};
Answer: Read a character string.
(b) Using strcpy function                            strcpy(string, “......”);
Answer: Copy one string to another.
(c) Reading using scanf function                scanf(“%s”,string);
Answer: It takes a string.
(d) Reading using gets function                            gets(string);
Answer: Read a line of string.
Compare them critically and describe situations where one is superior to others.

Question-8.6: Assuming the variable string  contain  the  value “ The sky is the limit ”, determine  
                  what output of the following segments will be.
(a) printf (“%s”,string);
output:
The sky is the limit
(b) printf(“%25.10s”,string);
output:
                  The sky is
(c) printf(“%s”,string[0]);
output:
.
(d) for(i=0;string[i]!= “.”,i++)
                printf(“%c”,string[i]);
output:

(e) for(i=0;string[i]!= ‘\0’;i++)
                printf(“%d\n”,string[i]);
output:

(f) for(i=0;i<=strlen[string]; ;)
      {
          string[i++]=i;
          printf(“%s\n”,string[i]);
       }
output:

(g) printf(“%c\n”,string[10]+5);
output:

(h) printf(“%c\n”,string[10]+5’);
output:


Question-8.7: Which of the following statements will correctly store  the  concatenation  of  strings 
                  s1 and s2 in string s3?
              (a) s3=strcat (s1,s2);
               Answer: correct.
(b) strcat(s1,s2,s3);
Answer: error.
(c) strcat(s3,s2,s1);
Answer: error.
(d) strcpy(s3,strcat(s1,s2));
Answer: correct.
(e) strcmp(s3,strcat(s1,s2));
Answer: correct.
(f) strcpy(strcat(s1,s2),s3);
Answer: error.

Question-8.8: What will be the output of the following statements?
                                          printf(“%d”,strcmp(“push”, “pull”));
                  output:
                  7

Question-8.9: Assume that s1, s2 and s3 are declared as follows:
                                          char s1[10]= “he”, s2[20]= “she”, s3[30], s4[30];
 What will be the output of following statements executed in sequence?
                                           printf(“%s”, strcpy(s3, s1));
                                           printf(“%s”, strcat(strcat(strcpy(s4,s1),“or”),s2));
                                           printf(“%d %d”,strlen(s2)+strlen(s3),strlen(s4));
                 output: 
                     he
                     heorshe
                     5  7 

Question-8.10: Find errors if any, in the following code segments;  
                    (a) char str[10]
                          strcpy(str, “GOD”, 3);
                          printf(“%s”,str);
                     Answer: error.
                     Correct answer: char str[10];
                                         strcpy(str, “GOD”, 3);
                                         printf(“%s”,str);

                    (b) char str[10];
                          strcpy(str, “Balagurusamy”);
                    Answer: no error.
                    (c) if strstr(“balagurusamy”, “guru”)==0);
                          printf(“Substring is found”);
                    Answer: no error.sss
                    (d) char s1[5], s2[10],
                          gets(s1, s2);
                     Answer: error.
                     Correct answer: char s1[5], s2[10];
                                         gets(s1);
                                            gets(s2);

Question-8.11: What will be the output of the following segment ?
                     char s1[]= “Kolkata”;
                     char s2[]=  “Pune”;
                     strcpy(s1, s2);
                     printf(“%s”,s);
                     output:
                     Pune

Question-8.12: What will be the output of the following segment s?
                     char s1[]= “NEW DELHI”
                     char s2[]= “BANGALORE”
                     strcpy(s1, s2, 3);
                     printf(“%s”, s1);
                     output:
                     BAN DELHI 
        
Question-8.13: What will be the output of the following code?
                     char s1[]= “Jabalpur”
                     char s2[]= “Jaipur”
                     printf(strncmp(s1, s2, 2))
                     output:
                     0

Question-8.14: What will be the output of the following code?
                     char s1[]= “ANIL KUMAR GUPTA”
                     char s2[]= “KUMAR”
                     printf(strstr(s1,s2));
                     output:
                     KUMAR GUPTA

Question-8.15: Compare the working of the following functions: 
(a)  stcpy and strncpy;
(b)   Answer: The function strcpy copies one string to another string but    the function strncpy    copies  only  the  left-most  n  characters of the  source  string  to  the target string
                      variable.  
  (b) strcmp and strncmp; and
  Answer: The strcmp function compares two  strings  identified  by  the  arguments  but
 strncmp function compares the left-most n characters of two string .
 (c) strcat and strncat
 Answer: The function   strcat  joins   two   strings   together   but  the  function  strncat
    concanate left-most n characters of target string to source string.



Programming exercise:



Question: Write a program which reads your name from the keyboard and output a list of ANCII codes, which represent your name.
Answer:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  int k,i,l;
  char dinar[100];
  printf("Enter a STRING:\n");
  gets(dinar);
  l=strlen(dinar);
  printf("the ASCII values of the string are:\n");
  for(i=0;i<l;i++)
          {
            k=dinar[i];
            printf("%d ",k);
          }
 }
Question: Write a program to do the following:
(a)  To output the question “Who is the inventor of C?”.
(b) To accept an answer.
(c)  To print out “Good” and then stop, if the answer is correct.
To output the massage ‘try again’ if the answer is wrong.
     (d)To display the correct answer when the answer is wrong even at the third attempt and stop.
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
            char s1[10];
            char s2[10]="Atiqur";
            int n;
            clrscr();
            printf("Who is the inventor of C ?");
            printf("\nAnswer is:");
            scanf("%s",s1);
            n=strcmp(s1,s2);
            if(n!=0)
             printf("try again");
            else
             printf("Good");
getch();
}

Question: Write a program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted, starting with the nth character.
Answer:
Question: Write a program which will read a text and count all occurances of a particular word.
Answer:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
const char* str;
const char c;
int num = 0;
int i;
clrscr();
const int end = strlen(str);
for(i = 0; i < end; ++i)
 {
if( str[i] == c )
{
++num;
}
}
getch();
}



Question: Write a program which will read a string and rewrite it in the alphabetical order. For example, the word STRING should be written as GINRST.
Answer:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[30],temp;
int n=0,j,i;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The string in alphabetical order is\n");
for(i=0;i<n;i++)
{
printf("%c",a[i]);
}
getch();
}

Question: Write a program to replace a particular word by another word in a given string. For example, the word “PASCAL” should be replaced by “C” in the text “It is good to program in PASCAL language”.
Answer:
#include<stdio.h>
#include<conio.h>
int stlen(char str[50])
{
int len = 0;
while(str[len]!='\0')
len++;
len;
return len;
}
void stcat(char str1[50], char str2[50])
{
int i = 0,len = 0;
while(str1[len]!='\0')
len++;
while(str2[i]!='\0')
{
str1[len] = str2[i];
i++;
len++;
}
str1[len] = '\0';
}
void main()
{
char str1[50], str2[50], str3[50], temp[50];
int len1, len2, len3, i, j, match, k;
clrscr();

printf("\n\n\t ENTER A SENTENCE: ");
gets(str1);
len1 = stlen(str1);
printf("\n\n\t ENTER A STRING WHICH YOU WANT TO DELETE:");
gets(str2);
len2 = stlen(str2);
printf("\n\n\t ENTER A NEW STRING WHICH YOU WANT TO INSERT : ");
gets(str3);
len3 = stlen(str3);
for(i=0;i<=len1;i++)
{
match = 1;
for(j=0;j<=len2;j++)
if(str2[j]!=str1[i+j])
{
match = 0;
break;
}
if(match)
{
for(k=0,j=i+len2+1;j<=len1;j++,k++)
temp[k] = str1[j];
temp[k] = '\0';
for(j=0;j<=len3;j++)
str1[i+j] = str3[j];
str1[i+j] = '\0';
stcat(str1,temp);
len1 = len1+len2+len3;
i = i + j;
}
}
printf("\n\n\t OUTPUT IS:" );
puts(str1);
getch();
}
Question: A Maruti car dealer maintains a record of sales of various vehicles in the following form:
         Vehicle type                 Month of sales                   price
         MARUTI-800                 02/01                                   210000
         MARUTI-DX                  07/01                                  265000
         GYPSY                            04/02                                  315750
         MARUTI-VAN               08/02                                  240000
Write a program to read this data into a table of strings and output the details of a particular vehicles sold during a specified period. The program should request the user to input the vehicle type and the period (starting month, ending month).

Question: Write a program that reads a string from the keyboard and determine whether the string is a palindrome or not. (A string is a palindrome if it can be read from left and right with the same meaning. For example, Madam and Anna are palindrome string. Ignore capitalization).
Answer:
#include <stdio.h>
#include <conio.h>
void main()
{
      int pelin(char[]);
      int flag;
      char str[30];
      clrscr();
      printf("*****PELINDROMESTRING*****\n\n");
      printf("Enter string : ");
      scanf("%s",str);
      if(pelin(str))
     printf("\nSTRING IS PELINDROME");
      else
     printf("\nSTRING IS NOT PELINDROME");

      getch();
}


int pelin(char str[]){
    int flag=0,i,j;
    char rev[30];
    for(i=0;str[i]!='\0';i++)
    rev[i] = str[i];
    //checking whether pelindrome
    i--;
    for(j=0;i>=0;j++,i--){
    if(rev[j]!=str[i]){
       flag=0;
       return(flag);
    }
    }
    flag=1;
    return (flag);
}

Question: Write a program that reads the cost of an item in the form RRRR.PP (Where RRRR denotes Rupees and PP denotes paisa) and converts the value to a string of words that expresses the numerical value in words. For example, if we input 125.75, the output should be “ONE HUNDRED TWENTY FIVE AND PAISA SEVENTY FIVE”.
Answer:
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main(){
    char init[27][12] = {" one "," two "," three ",
                " four "," five "," six ",
                " seven "," eight "," nine ",
                " ten "," eleven "," twelve ",
                " thirteen "," fourteen "," fifteen ",
                " sixteen "," seventeen "," eighteen ",
                " nineteen "," twenty "," thirty ",
                " fourty "," fifty "," sixty ",
                " seventy "," eighty "," ninty "};
    char sthou[20]="",shund[20]="",sval1[20]="",sval2[20]="",result[100]="";
    int thou=0,hund=0,ten=0,temp=0,val1,val2,num,rem,c=0;


//USING COBOL LOGIC by dinar
     printf("*****AMOUNT IN WORDS*****\n\n");
     printf("Enter any value (upto 4 digits) : ");
     scanf("%d",&num);
     while(num>0){
         rem = num%10;
         c++;
         if(c<=2)
        temp = temp * 10  +rem;
         else if(c==3)
        hund=rem;
         else if(c==4)
         thou=rem;
         num=num/10;
     }
     while(temp>0){  //as ten contains two digit so reverse it
        rem = temp%10;
        ten = ten * 10 + rem;
        temp= temp/10;
     }

     if(thou>0){
       strcpy(sthou,init[thou-1]);
       strcat(sthou," thousand ");
       strcat(result,sthou);
     }

     if(hund>0){
       strcpy(shund,init[hund-1]);
       strcat(shund," hundred ");
       strcat(result,shund);
     }

     if(ten>0){
          if(ten>20){
          val1 = ten/10;
          val2 = ten%10;
          }
          if(val1>0){
           strcpy(sval1,init[val1+(18-1)]);
           strcat(result,sval1);
          }
          if(val2>0){
           strcpy(sval2,init[val2-1]);
           strcat(result,sval2);
          }
     }
     printf("\n\nAmount in word is as under \n");
     printf("%s",result);

getch();
}


Question: Develop a program that will read and store the details of a list of students in the format
              Roll No.                            Name                             Marks obtaine d
             …………..                      …………                      ………………..
             …………..                      …………                       ………………..
             …………..                        ………….                       …………………
            …………..                        ………..                       …………………                                                                   
And produce the following output lits:
(a)  Alphabetical list of names, roll numbers and marks obtained.
(b) List sorted on roll numbers.
(c)  List sorted on marks (rank-wise list).
Question:
Write a program to read strings and compare them using the function strncmp() and print the massage that the first string is equal, less or greater than the second one.
Answer:
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
int x;
printf("\n\nEnter two string constants\n");
printf("?");
scanf("%s %s",s1,s2);
x=strcmp(s1,s2);
if(x==0)
{
printf("\n\nStrings are   equal\n");
 }

 else if(x<0)
 {
printf("String 1 is less than string 2");
 }
else
 {
printf("String 1 is greater than String 2");
}

getch();
}

Question: Write a program to read a line of text from the keyboard and print out the number of occurrences of a given substring using the function strstr().
Answer:
 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  char a[100],b[100];
  char n,sazon;
  printf("Input two string:\n");
  gets(a);
  n=strlen(a);
  gets(b);
  sazon=strncmp(a,b,n);
  if(dinar==0)
           printf("equal.");
  else
           if(sazon>0)
                   printf("a>b");
           else
                   printf("a<b");
getch();
}
Question: Write a program that will copy m consecutive characters from a string s1 beginning at position n into another string s2.
Answer:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
            char str1[20],str2[20],ch;
            int i;
            clrscr();
            printf("*****Strings Copy Function*****\n\n");
            printf("Enter string1: ");
            scanf("%s",str1);
            printf("\n\nUsing Inbuilt Function");
            strcpy(str2,str1);
            printf("\nString1 : %s\nString2 : %s\n",str1,str2);

            strcpy(str2,"");  //empty to copy again
            printf("\n\nWithout Using Inbuilt Function");
            for(i=0;str1[i]!='\0';i++)
           str2[i] = str1[i];
            str2[i]='\0';
            printf("\nString1 : %s\nString2 : %s\n",str1,str2);

            getch();

}

Question: Write a program to create a directory of students with roll numbers. The program should display the roll number for a specified name and vice-verse.

Question: Given that
                  char str[ ]= “123456789”;
Write a program that displays the following:
                 1
               232
             34543
          4567654
        567898765   
Answer:
#include<stdio.h>
#include <conio.h>
void main()
{
            int i,j,k,m,num,c,tmp;
            clrscr();
            printf("*****PYRAMID*****\n\n");
            printf("Enter num of lines : ");
            scanf("%d",&num);
            for(i=1;i<=num;i++){
            c=i;
            for(k=1;k<=(num-i);k++)
                     putchar(' ');

            for(j=i;j>=1;j--){
                             if(c>9){
                               printf("%d",0);
                               tmp=c;
                             }
                             else{
                             printf("%d",c++);
                             tmp=c;
                             }
                   }
           tmp--;

            for(m=tmp;m>i;)
                     printf("%d",--m);

            printf("\n");
            }
getch();
}