Friday, 28 February 2014

Use of Local and global variable in function with example

Local and global variable
Local variable is defined within the body of the fucntion.Other function cannot access these variables.
Global variables-These are defined outside main () fucntion.Mulitiple functions can use them.

Write a program to show local variables.
#include<stdio.h>
#include<conio.h>
main()
{
    int a=10,b=20;
    clrcsr();
    printf("a=%d , b=%d",a,b);
}
func()
{
    int a=20,b=10;
    printf("a=%d b=%d",a,b);
}
Output
a=10,b=20
a=20,b=10
//here variable a and b are defined in both functions,There effect is only within the function in which they are defined//

Write a program to show global variables.
#include<stdio.h>
#include<conio.h>
int a =10,b=10;
main()
{
    clrcsr();
    printf("a=%d , b=%d",a,b);
    func();
    printf("a=%d , b=%d",a,b);
}
func()
{
   
    a++;   
    b--;
}
Output
a=10  ,b=10
a=11  ,b=9

Explain function with example.

Function-A function is a sub block with one or two statments which perform a task when called.Whenever a fucntion is called control passes to called function from calling function.Control returns back to calling function only when execution is done.The values of actual arguments are passed to formal arguments by calling function.
The function performs on formal arguments.
Syntax
functionname(arguments)
    {
    local variable declaration;
    stmt;
    return value;
    }
Write a program to show how a function is called.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x=1,y=2,z;
z=add(x,y); //calling function with actual arguments x,y//
printf("z=%d",z);
}
add(a,b)
{
return(a+b);//called function with formal arguments a,b//
}

Friday, 31 January 2014

Explain strchr() and strstr() function with example.

strchr() function-This function returns pointer to a position of the first occurence of specified character in the given string.
syntax
chp=strchr(string,ch)   
chp is the pointer,ch is the character to be find in the string

Write a program to find a character in the string using strchr() function.

#include<stdio.h>
#include<conio.h>
main()
{
char string[10],*chp,ch;
clrscr();
printf("Enter the string");
gets(string);
printf("\n Enter the character to be find in the string:");
ch=getchar();
chp=strchr(string,ch);
if(chp)
    {
    printf("\n Character %c is found in the string",ch);
    }
        else
    {
    printf(""\n Character %c is not found in the string",ch);
    }
}
Output
Enter the string:Hello Begginers
Enter the character to be find in the string:r
Character r is found in the string

strstr() function-This function finds the second string in the given string.
syntax
strstr(str
ing1,string2)

Write a program to find a character in the string using strchr() function.

#include<stdio.h>
#include<conio.h>
main()
{
char string1[10],string2[10],*chp;
clrscr();
printf("Enter the first string");
gets(string1);
printf("\n Enter the second string to be found in the first string:");
gets(string2);
chp=strtr(string1,string2);
if(chp)
    {
    printf("\n second string %s is found in the first string",string1);
    }
        else
    {
    printf(""\n second string %s is not found in the first string",string2);
    }
}
Output
Enter the first string:Hello Begginers
Enter the second string to be found in the first string:Hello
second string Hello is found in the first string

Wednesday, 29 January 2014

Explain strdup() function.

strdup() function-This function is used for duplicating the string at allocated memory which is pointed by a pointer variable.
syntax
dup1=strdup(s1) ,where s1 is string and dup1 is pointer
Write a program to enter a string and get its duplicate with strdup() function.
#include<stdio.h>
#include<conio.h>
main()
{
    char string[10],dup1[10];
    clrscr();
    printf("Enter the string:");
    gets(string);
    dup1=strdup(string)
    printf("\n Original string %s \n Duplicate string %s:",string,dup1);
}
Output
Enter the string:Good Day
Original string Good Day
Duplicate string Good Day
   
   



 

Explain strlwr(),strupr() function with example.

strlwr() function-This function converts any string to a lower case.
syntax
strlwr(upper);

Write a program to convert uppercase string to lowercase.
#include<stdio.h>
#include<conio.h>
main()
{
    char upper[10];
    clrscr();
    printf("Enter the string in uppercase:");
    gets(upper);
    printf("After strlwr(): %s",strlwr(upper));
}
Output
Enter the string in uppercase: ABCD
After strlwr(): abcd


strupr() function-This function converts any string to a upper case.
syntax
strupr(lower);

Write a program to convert lowercase string to uppercase.
#include<stdio.h>
#include<conio.h>
main()
{
    char lower[10];
    clrscr();
    printf("Enter the string in lowercase:");
    gets(lower);
    printf("After strupr(): %s",strupr(lower));
}
Output
Enter the string in uppercase: abcd
After strlwr(): ABCD

Explain stricmp(),strcmp(),strncmp() function.

stricmp() function-This function compares between two strings,it doesn't discriminates between lower and upper case.
syntax
stricmp(source string,destination string)

Write a program to compare between two strings.
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    int diff;
    clrscr();
    printf("Enter the source string:");
    gets(s1);
    printf("Enter the destination string:");
    gets(s2);
    diff=stricmp(s2,s1);
    if(diff==0)
        {
            printf("Two strings are identical");
        }
        else
        {
            printf("Two strings are not identical");
        }
}
Output
Enter the source string:HELLO
Enter the destination string:hello
Two strings are identical


strcmp() function-This function compares between two strings,it does discriminates between lower and upper case.
syntax
stricmp(source string,destination string)

Write a program to compare between two strings.
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    int diff;
    clrscr();
    printf("Enter the source string:");
    gets(s1);
    printf("Enter the destination string:");
    gets(s2);
    diff=strcmp(s2,s1);
    if(diff==0)
        {
            printf("Two strings are identical");
        }
        else
        {
            printf("Two strings are not identical");
        }
}
Output
Enter the source string:HELLO
Enter the destination string:hello
Two strings are not identical

strncmp() function-This function compares between two strings upto specified length.
syntax
stricmp(source string,destination string,n)

Write a program to compare between two strings upto specified length.
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    int n,diff;
    clrscr();
    printf("Enter the source string:");
    gets(s1);
    printf("Enter the destination string:");
    gets(s2);
    printf("Enter length upto which comparison is to be made");
    scanf("%d",&n);
    diff=strcmp(s2,s1,n);
    if(diff==0)
        {
            printf("Two strings are identical upto %d characters");
        }
        else
        {
            printf("Two strings are not identical");
        }
}
Output
Enter the source string:HELLO
Enter the destination string:hello
Two strings are not identical

Explain strcpy() and strncpy() function with example.

strcpy() function-It copies contents of one string to another.
Syntax
strcpy(destination,source)  ,where source string is copied to destination string.

Write a program to copy contents of one string to another by using strcpy().
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    clrscr();
    printf("Enter the string:");
    gets(s1);
    strcpy=(s2,s1);
    printf("\n Original string :%s",s1);
    printf("\n Duplicate string :%s",s2);
}
Output
Enter the string:Ragu
Original string:Ragu
Duplicate string:Ragu

strncpy() function-This function copies specified length of characters from source to destination string.
syntax
strncpy(destination,source,n) ,where n is the specified length

Write a program to copy source string to destination string with given length.
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    int n;
    clrscr();
    printf("Enter the source string:");
    gets(s1);
    printf("Enter the destination string:");
    gets(s2);
    printf("Enter number of characters to be replaced in destination string:");
    scanf("%d"&n);
    strcpy=(s2,s1,n);
    printf("\n Original string :%s",s1);
    printf("\n Duplicate string :%s",s2);
}
Output
Enter the source string:Wonderful
Enter the destination string:beautiful
Enter number of characters to be replaced in destination string:6
Enter the source string:Wonderful
Enter the destination string:wonderful