Monday, 6 January 2014

C PROGRAM TO DEVELOP STRING FUNCTION -- strcpy

#include<stdio.h>
#include<string.h>
#include<conio.h>

void  my_strcpy(char *dest , char *source);


int main()
{
    char str[20];
    char dest[20];
    printf("enter any string:");
    scanf("%s",str);
   
    my_strcpy(dest,str);
   
    printf("\ndest=%s",dest);
   
    getch();
    return 0;
}



void my_strcpy(char *dest,char *source)
{
     while(*source!='\0')
     {
                         *dest++=*source++;
                         }
                         *dest='\0';
                        
                         }

C PROGRAM TO DEVELOP SORTING THE STRINGS IN ALPHABETICAL ORDER

#include<stdio.h>
#include<string.h>
#include<conio.h>


int main()
{
    char str[10][20];
    char temp[20];
    char ch;
    char *dest;
    int i=0,j=0,k=0;
  
  
    while(strlen(gets(str[i++]))!=0)
    ;
    i--;
  
    while(k<=i)
    {
    for(j=k;j<=i-1;j++)
    {
                    if((strcmp(str[k],str[j]))>0) //swap
                    {
                                                    strcpy(temp,str[j]);
                                                    strcpy(str[j],str[k]);
                                                    strcpy(str[k],temp);
                                                    }
                                                    }
                                                    k++;
                                                    }
                                                  
                                                  
for(j=0;j<=i;j++)
puts(str[j]);

getch();
return 0;
}                                                  

C PROGRAM TO DEVELOP STRING FUNCTION -- strrchr

#include<stdio.h>
#include<string.h>
#include<conio.h>

char *my_strrchr(char *string,char charecter);

int main()
{
    char str[20];
    char ch;
    char *dest;
   
    printf("enter any string:");
    gets(str);
   
    printf("enter an charecter to search in the string:");
    scanf("%c",&ch);
   
    dest=my_strrchr(str,ch);
   
    printf("\n dest=%s",dest);
   
    getch();
    return 0;
}


char *my_strrchr(char *string,char charecter)
{
     char *temp;
     int flag=0;
     while(*string!='\0')
     {
                         if(*string==charecter)
                         temp=string;flag=1;
                        
                         string++;
                         }
                        
                         if(flag==1)
                         return(temp);
                         else
                         return(NULL);
                         }

C PROGRAM TO DEVELOP STRING FUNCTION- strchr

#include<stdio.h>
#include<string.h>
#include<conio.h>

char *my_strchr(char *string,char charecter);

int main()
{
    char str[20];
    char ch;
    char *dest;
   
    printf("enter any string:");
    gets(str);
   
    printf("enter an charecter to search in the string:");
    scanf("%c",&ch);
   
    dest=my_strchr(str,ch);
   
    if(dest==NULL)  
    printf("\nvirus");
    else
    printf("\nno virus");
   
    getch();
    return 0;
}



char *my_strchr(char *string,char charecter)
{
     while(*string!='\0' && *string!=charecter)
     {
                         string++;
                         }
                        
                         if(string==NULL)
                         return(NULL);
                         else
                         return(string);
                         }