Delicious Digg Facebook Favorites More Stumbleupon Twitter

Tuesday, 9 October 2012

String functions programs


       Program for comparison between two strings :-

       #include<stdio.h>
       #include<string.h>
       main()
       {    
               char a[100], b[100];   
               printf("Enter the first string\n");   
               gets(a);   
               printf("Enter the second string\n");   
               gets(b);   
               if( strcmp(a,b) == 0 )      
               printf("Entered strings are equal.\n");   
               else      
               printf("Entered strings are not equal.\n");   
               return 0;
         }

       Program for copy of two strings :-

       #include<stdio.h>
       #include<string.h>
       main()
       {
               char source[] = "C program";   
               char destination[50];   
               strcpy(destination, source);   
               printf("Source string: %s\n", source);   
               printf("Destination string: %s\n", destination);   
               return 0;
        }

       Program for String Reverse :-
       
       #include<stdio.h>
       #include<string.h>
       main()
       {   
               char arr[100];   
               printf("Enter a string to reverse\n");   
               gets(arr);   
               strrev(arr);   
               printf("Reverse of entered string is \n%s\n",arr);   
               return 0;
        }


        Program for Append the two Strings :-

        #include<stdio.h>
        #include<conio.h>
        #include<string.h>
        main()
        {   
               char a[100], b[100];   
               printf("Enter the first string\n");   
               gets(a);   
               printf("Enter the second string\n");   
               gets(b);   
               strcat(a,b);
               printf("String obtained on concatenation is %s\n",a);   
               getch();   
               return 0;
         }

string functions

     
        
Strings are used in string handling operations such as,
  • Counting the length of a string.
  • Comparing two strings.
  • Copying one string to another.
  • Converting lower case string to upper case.
  • Converting upper case string to lower case.
  • Joining two strings.
  • Reversing string.

STRINGS STANDARD FUNCTION:-
----------------------------------------------
strlen():-determines length of a string.

strcpy():-copy a string from source 2 destination.

strncpy():-copy achars of a string 2 another string up 2 specified length.

strcmp():-compares 2 strings(depends on b/w small & capital).

stricmp():-compares 2 strings(doesn't depends upon b/w small & capital).

strncmp():-compares 2 strings of specified lengtgh.

strnicmp():-compares 2 strings(doesn't depends upon b/w small & capital)of specified lengtgh.

strlwr():-converts uppercase string 2 lowercase string.

strupr():-converts lowercase string 2 uppercase string.

strdup():-duplicate a string.

strchr():-determines 1st occurance of a given char in a string.

strrchr():-determines lost occurance of a given char in a string.

strstr():-determines 1st occurance of a given string in another string.

strcat():-appends source string 2 destination string.

strncat():-appends source string 2 destination string upto specified length.

strrev():-reverse all charecters of astring.

strset():-sets all chars of astring with a given argument or symbol.

strnset():-sets all chars of astring with a given argument or symbol upto specified length.

strspn():-finds upto what length two strings are identical.


 

Monday, 8 October 2012

string examples

#include<string.h>
main()
{
     char arr1[9]={'w','e','l','','c','o','m','e'};
     char arr2[9]="welcome";
     char arr3[9]={{'w'},{'e'},{'l'},{''},{'c'},{'o'},{'m'},{'e'}};
     clrscr();
     pf("\narray1=%s",arr1);
     pf("\narray2=%s",arr2);
     pf("\narray3=%s",arr3);
}
 
output :-
                array1= wel come
                array2= welcome
                array3= wel come

example for counting elements :-
   
#include<string.h>
main()
{
    char text[17]="RANDOM ACCESS";
    clrscr();
    printf("%s\n",text);
    printf("%.5s\n",text);
    printf("%.8s\n",text);
    printf("%.15s\n",text);
    printf("%17s\n",text);
    printf("%-10.4s\n",text);
    getch();
}

output :-

RANDOM ACCESS
RANDO
RANDOM A
RANDOM ACCESS
RANDOM ACCESS
RANDOM ACC

strings in c-language

                                              STRINGS
 
    
String :-

A string is a collection of characters. Strings are always enlosed in double quotes as "string_constant".
 


  • In C language Strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL '\0' character:
  • It is important to preserve the NULL terminating character as it is how C defines and manages variable length strings. All the C standard library functions require this for successful operation.
  • All the string handling functions are prototyped in: string.h or stdio.h standard header file. So while using any string related function, don't forget to include either stdio.h or string.h. May be your compiler differs so please check before going ahead.
  • If you were to have an array of characters WITHOUT the null character as the last element, you'd have an ordinary character array, rather than a string constant.
  • String constants have double quote marks around them, and can be assigned to char pointers as shown below. Alternatively, you can assign a string constant to a char array - either with no size specified, or you can specify a size, but don't forget to leave a space for the null character!

  • Declaration of Strings :-

         datatype variable name array length=value;

    example :-  datatype is char, converted into string by using the
                       systemcode "%s",

                       char  a[10]={"hello"};
                                    (or)
                       char a[10]={'h','e','l','l','o'};
                                    (or)
                       char a[10]={{'h'},{'e'},{'l'},{'l'},{'o'}};
                    
                       in strings space( ) , .  is also counted.


     

    check addresses are correct or not


        /* cosider 3 variables and find the addresses ,first two variables
            addresses difference is equal to another two variables  
            addresses  difference. */

    #include<stdio.h>
    main()
    {
    int x=10,y=20,z=30;
    clrscr();
    printf("address of x=%d\n",&x);
    printf("value of x=%d\n",x);
    printf("address of y=%d\n",&y);
    printf("value of y=%d\n",y);
    printf("address of z=%d\n",&z);
    printf("value of z=%d\n",z);
    getch();
    }

    output :-
    address of x= -12
    value of x= 10
    address of y= -14                       
    value of y= 20
    address of z= -16
    value of z= 30



    pointer examples

     
       
    1)   Meaning of following simple pointer declaration and definition:
     
    int a=5;
    int * ptr;
    ptr=&a;
    Explanation:
    About variable a:
    1. Name of variable: a
    2. Value of variable which it keeps: 5
    3. Address where it has stored in memory: 1025 (assume)
    About variable ptr:
    4. Name of variable: ptr
    5. Value of variable which it keeps: 1025
    6. Address where it has stored in memory: 5000 (assume)
    Pictorial representation:
     
     
     
     
     
    Note: A variable where it will be stored in memory is decided by operating system. We cannot guess at which location a particular variable will be stored in memory.
    (2) Meaning of following pointer declaration and definition:
    int a=50;
    int *ptr1;
    int **ptr2;
    ptr1=&a;
    ptr2=&pt1;
     
    Explanation:
    About variable a:
    1. Name of variable: a
    2. Value of variable which it keeps: 50
    3. Address where it has stored in memory: 5000 (assume)
    About variable ptr1:
    4. Name of variable: ptr1
    5. Value of variable which it keeps: 5000
    6. Address where it has stored in memory: 9000 (assume)
    About variable ptr2:
    7. Name of variable: ptr2
    8. Value of variable which it keeps: 9000
    9. Address where it has stored in memory: 9555 (assume)
    Pictorial representation of above pointer declaration and definition:
     

     
    Note:
     
    ‘*‘is known as indirection operator which gives content of any variable.
    & is known as reference operator which gives address where variable has stored in memory.
     
    Cancellation rule of above two operators:
     
    * and & operators always cancel to each other i.e.
    *&p=p
    But it is not right to write:
    &*p=p
    Simple example:
     
    What will be output of following c program?
    #include<stdio.h>
    int main(){
    int x=25;
    int *ptr=&x; //statement one
    int **temp=&ptr; //statement two
    printf(“%d %d %d”.x.*ptr,**temp);
    return 0;
    }
     
    Output: 25 25 25
    Explanation:
    As we know value of variable x is 25.
     
    *ptr= *(&x) //from statement one
    =*&x
    =x //using cancellation rule
    =25
    **temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25
    Example :-
    #include<stdio.h>
    main()
    {
    int x=10,*ptr;
    clrscr();
    ptr = &x;//(address of the variable)
    *ptr = x;//(value of the variable)
    printf("address of x=%d\n",ptr);
    (or)
    printf("address of x=%d\n",&x);
    printf("value of x=%d\n",*ptr);
    (or)
    printf("value of x=%d\n",x);
    getch();
    }
    output :-
    address of x= -12
    address of x= -12
    value of x= 10
    value of x= 10