WELCOME

Welcome to KoDes4U

Pages

Friday, February 25, 2011

string operation without using library function(c programs)



/*Write a program on string operations without using library functions
   coded by KoDes4U.blogspot.com*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
int length(char str[]);
void copy(char str[]);
int compare(char str1[],char str2[]);
void reverse(char str[]);
int pallindrme(char str[]);
void concat(char str1[],char str2[]);
int search(char str1[],char str2[]);
void count(char str[]);


void main()
{
 char str1[50],str2[50];
 int res,op;
 clrscr();
  do
 {
  printf("\n-------------------------------MENU-----------------------------");
  printf("\n1)Length\n2)Copy\n3)Compare\n4)Reverse\n5)Pallindrome");
  printf("\n6)Concatenate\n7)Search\n8)Count\n9)Exit");
  printf("\nEnter your choice:");
  scanf("%d",&op);
  flushall();
  switch(op)
  {
   case 1:printf("\nEnter any string:");
 gets(str1);
 res=length(str1);
 printf("\nLength of the string is: %d",res);break;
   case 2:printf("\nEnter any string:");
 gets(str1);
 copy(str1);
 break;
   case 3:printf("\nEnter 1st string:");
 gets(str1);
 printf("\nEnter 2nd string:");
 gets(str2);
 res=compare(str1,str2);
 if(res>0)
 printf("\n1st string is greater.");
 else if(res<0)
 printf("\n2nd string is greater.");
 else
 printf("\nBoth are same.");
 break;
   case 4:printf("\nEntre any string:");
 gets(str2);
 reverse(str1);
 break;
   case 5:printf("\nEnter any string:");
 gets(str1);
 res=pallindrome(str1);
 if(res==0)
 printf("\nPallindrome.");
 else
 printf("\nNot a pallindrome.");
 break;
   case 6:printf("\nEnter main string:");
 gets(str1);
 printf("\nEnter substring:");
 gets(str2);
 concat(str1,str2);break;
   case 7:printf("\nEnter the main string:");
 gets(str1);
 printf("\nEnter substrring:");
 gets(str2);
 res=search(str1,str2);
 printf("\nFound at location:%d",res);
 break;
   case 8:count(str1);
 break;
   case 9:break;
  }
 }              while(op!=9);
}


int length(char str[])
{
 int i=0;
 while(str[i]!='\0')
 i++;
 return(i);
}
void copy(char str[])
{
 char a[50];
 int i=0;
  printf("\nOriginal string is.%s",str);
 while(str[i]!='\0')
 {
 a[i]=str[i];
 i++;
 }
 printf("\nCopied string is:%s",a);
}
int compare(char str1[],char str2[])
{
 int i=0;
 while(str1[i]!='\0')
 {
  if(str1[i]>str2[i])
  return(1);
  if(str1[i]<str2[i])
  return(-1);
  i++;
 }
 return(0);
}


void reverse(char str[])
{
 int i=0,j=0;
 char temp[50];
 while(str[i]!='\0')
 i++;
 i--;
// n=i;
 //i=0;
 printf("\nOriginal string is:%s",str);
 //i=0;
 while(i>=0)
 {
  temp[j]=str[i];
  i--;
  j++;
 }
  printf("\nReversed string is:%s",temp);
}


void concat(char str1[],char str2[])
{
 int i=0,j=0;
 while(str1[i]!='\0')
 i++;
 printf("\nBefore concatenation:");
 printf("\n1st string is:%s",str1);
 printf("\n2nd string is:%s",str2);
 while(str2[j]!='\0')
 {
  str1[i]=str2[j];
  i++;j++;
 }
 printf("\nConcatenated string is:%s",str1);
}


int pallindrome(char str[])
{
 int i=0,j=0;
 while(str[j]!='\0')
 j++;
 j--;
 while(j>i)
 {
  if(str[i]!=str[j])
  return(1);
  i++;
  j--;
 }
  return(0);
}
int search(char str1[],char str2[])
{
 int i,j,lstr1,lstr2;
 lstr1=length(str1);
 lstr2=length(str2);
 for(i=0;i<=lstr1-lstr2+1;i++)
  for(j=0;str1[i+j]==str2[j]&&str2[j]!='\0';j++)
   if(str2[j]=='\0')
   return(i+1);
   return(0);
}
void count(char str[])
{
 int spcnt=0,chcnt=0,i;
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]==' ')
  spcnt++;
  chcnt++;
 }
 printf("\nNo. of spaces=%d",spcnt);
 printf("\nNo. of words=%d",spcnt+1);
 printf("\nNo. of characters=%s",chcnt);
}

No comments:

Post a Comment