Wednesday, August 17, 2011

QUES 8 STRING REVERSE,POSITION OF CHARACTER AND LENGTH


C LANGUAGE BOOK








WAP TO COMPUTE THE STRING REVERSE,LENGTH OF THE STRING AND
FIND THE POSITION OF CHARACTER ENTERED BY THE USER.



#include<stdio.h>
#include<conio.h>
void main()
{
  int xstrlen(char *);
  int len1,len2,pos;
  char a[30],ch;

  clrscr(); //CLEARS THE OUTPUT SCREEN
  printf("enter the string: ");
  gets(a); //TAKING THE STRING
  len1= strlen (a);         //THE ORIGINAL FUNCTION
  len2= xstrlen (a);           //FUNCTION MADE BY USER
  printf("\n the length of the string is");
  printf("\t%d" ,len1); //PRINT THE LENGTH OF STRING



  printf("enter the character ");
  scanf("%c",&ch);
  pos=xstrchr(len,ch,a);
  printf("position of character is %d",pos);

 xstrev(a);
 printf("reverse string is %s",a);

  getch();
}

//FUNCTION TO FIND LENGTH OF THE STRING.
int xstrlen(char *s) //FUNC. TAKING CHAR POINTER
{
  int length=0;  //LENGTH IS INITIALISED WITH ZERO TO REMOVE GARBAGE VALUE
  while(*s!='\0')
  {
    length++; //INCREMENT THE LENGTH
    s++;     //INCREMENT THE S
  }
  return(length);  //RETURNS THE LENGTH
}




//FUNCTION TO FIND THE POSITION OF CHARACTER IN STRING.
int xstrchr(int len,char ch, char*s)
{
int t,i;

for(i=0;i<len;i++)
{
if(ch==(*s))
{
t=i;
break;
}
else
  s++;
}

return t;
}




//FUNCTION TO REVERSE THE STRING.
void xstrev(char*s)
{
char *t,temp;
int l,i;
l=strlen(s);
t=s+l-1;
for(i=1;i<=l/2;i++)
{
temp=*s;
*s=*t;
*t=temp;
s++;
t--;
}
}






C LANGUAGE BOOK

No comments:

Post a Comment