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

Explain strlen() function.

strlen() function-It counts number of characters in a string.
Syntax
l=strlen(string)   ,where l reads the count of characters

Write a program to read a string through keyboard.Determine length of the string.

#include<stdio.h>
#include<conio.h>
main()
{
    char string[10];
    int string,l;
    printf("Enter the string:");
    gets(string);
    l=strlen(string);
    printf("Entered name is %s and its length is %d",string,l);
}
Output
Enter the string:
Rohini
Entered string is Rohini and its length is 6

Explain string with different formats.

String
Group of characters,digits,symbols enclosed within quotation marks are called strings.The C compiler inserts NULL(\0) character automatically at end of the string.
Different formats of initialization of string
char name[]="INDIA";
or
char name[]={'I','N','D','I,'A','\0'};
or
char name[]={{I},{N},{D},{I},{A}};

Write a program to print "INDIA" by using different formats of initialization of array.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
   {
    char a1[]={'I','N','D','I''A','\0'};
    char a2[]="INDIA";
    char a3[6]={{I},{N},{D},{I},{A}};
    clrscr();
    printf("\n Array1=%s",a1);
    printf("\n Array2=%s",a2);
    printf("\n Array3=%s",a3);
   }
Output
Array1=INDIA
Array2=INDIA
Array3=INDIA

Write a program to display string 'PRABHAKAR 'using different formats.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[]="PRABHAKAR";
clrscr();
printf("  %s  \n ",a);
printf(" %.5s \n ",a);
printf(" %.8s \n ",a");
printf(" %.15s \n ",a);
printf(" %-10.4s  \n",a);
printf(" %11s \n ",a);
}
Output
PRABHAKAR
PRABH
PRABHAKA
PRABHAKAR
PRAB
PRABHAKAR

Print elements of character array given with while loop (help of Null character).

#include<stdio.h>
#include<conio.h>
main()
{
    char a[]="Have a nice day";
    int i=0;
    clrscr();
    printf("Elements of the array:");
    printf("\n");
    while(a[i]!='\0')
        {
        printf("  %c ",a[i]);
        i++;   
        }

}
Output
Elements of the array:
Have a nice day