Friday, May 30, 2014

WRITE A PROGRAMME TO IMPLEMENT THE LIBRARY FUNCTIONS-STRLEN,STRCMP,STRLWR,STRUPR,STRREV,STRCPY,STRCAT USING POINTER

//WRITE A PROGRAMME TO IMPLEMENT THE FOLLOWING LIBRARY FUNCTIONS-
//STRLEN,STRCMP,STRLWR,STRUPR,STRREV,STRCPY,STRCAT
//String & Pointer

#include<stdio.h>
#include<conio.h>
int length(char*);
int comp(char*,char*);
void lower(char*);
void upper(char*);
void reverse(char*);
void copy(char*,char*);
void concat(char*,char*);

void main()
{
char s1[20],s2[20],s3[20],s4[20];
int l,i;
clrscr();
printf("enter the first string\n");
gets(s1);
printf("enter the second string\n");
gets(s2);


printf("the length of %s string is- ",s1);
l=length(s1);
printf("%d\n",l);


printf("the result of compairing first and second string-\n");
i=comp(s1,s2);
if(i==0)
{
printf("\n%s and %s are equal\n",s1,s2);
}
else
{
printf("\n %s and %s are not equal with a difference of %d in their ascii value\n",s1,s2,i);
}


printf("\nthe %s string after changing case to lower- ",s1);
lower(s1);
printf("%s\n",s1);


printf("\nthe %s string after changing case to upper- ",s1);
upper(s1);
printf("%s\n",s1);

printf("\n the %s string after being reversed- ",s1);
reverse(s1);
printf("%s\n",s1);


printf("\n %s after being copied by %s is- ",s1,s2);
copy(s1,s2);
printf("%s\n",s1);


printf("enter two strings for applying concatination operation\n");
printf("enter the first string\n");
gets(s3);
printf("enter the second string\n");
gets(s4);
printf("\n%s after being concatenated with %s- ",s3,s4);
concat(s3,s4);
printf("%s\n",s3);


getch();
}

int length(char *p)
{
int i=0;
while(*p)
{
i++;
p++;
}
return(i);
}

int comp(char *p,char *q)
{
int i=0;
while(p[i] && q[i] && (p[i]==q[i]))
{
i++;
}
if(p[i]==q[i])
return 0;
else if(p[i]>q[i])
return(p[i]-q[i]);
else
return(q[i]-p[i]);
}

void lower(char *p)
{
int i=0;
while(p[i])
{
if(p[i]>=65 && p[i]<=91)
 {
   p[i]=p[i]+32;
 }
 i++;
}
}

void upper(char *p)
{
int i=0;
while(p[i])
{
if(p[i]>=97 && p[i]<=123)
 {
   p[i]=p[i]-32;
 }
 i++;
}
}

void reverse(char *p)
{
int i,j,len=strlen(p);
char ch;
for(i=0,j=len-1;i<j;i++,j--)
{
ch=p[i];
p[i]=p[j];
p[j]=ch;
}
}

void copy(char *p,char *q)
{
while(*q)
{
*p=*q;
q++;
p++;
}
if(*p!=0)
*p=0;
}

void concat(char *p,char *q)
{
int i=0,j=0;
while(p[i])
{
i++;
}
p[i]=32;
i++;
while(q[j])
{
p[i]=q[j];
i++;
j++;
}
p[i]='\0';
}

WRITE A PROGRAMME TO COUNT THE NO OF WORDS IN A STRING

#include<conio.h>
#include<stdio.h>
void main()
{
char s1[50];
int i=0,j,c=0;
clrscr();
printf("enter the string\n");
gets(s1);
while(s1[i])
{
if(s1[i]==32)
 {
  c++;
if(s1[i+1]==32)
{
 j=i+1;
 while(s1[j]==32)
 {
    j++;
    continue;
 }
 i=j;
}
  }
i++;
}
printf("no of words in %s are %d",s1,++c);
getch();
}

WRITE A PROGRAMME TO INVERT UPPERCASE LETTERS TO LOWERCASE LETTERS AND VICE VERSA IN A STRING

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20];
int i=0;
clrscr();
printf("enter the string\n");
gets(s1);
while(s1[i])
{
if(s1[i]>='A'&& s1[i]<='Z')
s1[i]=s1[i]+32;
else if(s1[i]>='a' && s1[i]<='z')
s1[i]=s1[i]-32;
i++;
}
puts(s1);
getch();
}

WRITE A PROGRAMME TO COUNT NO OF VOWELS,CONSONANTS AND NO OF DIGITS IN A STRING

#include<conio.h>
#include<stdio.h>
void main()
{
char ar[20];
int v=0,c=0,d=0,i=0;
clrscr();
printf("enter the string\n");
gets(ar);
while(ar[i])
{
if(ar[i]!=32)
{
if(ar[i]>='0'&&ar[i]<='9')
d++;
else if(ar[i]=='a'||ar[i]=='e'||ar[i]=='i'||ar[i]=='o'||ar[i]=='u')
v++;
else
c++;
}
i++;
}
printf("\nno of vowels in %s are-%d",ar,v);
printf("\nno of consonants in %s are-%d",ar,c);
printf("\nno of digits in %s are-%d",ar,d);
getch();
}

WRITE A PROGRAMME TO COPY ONE STRING INTO ANOTHER STRING

#include<conio.h>
#include<stdio.h>
void main()
{
char s1[20],s2[20];
int i=0;
clrscr();
printf("enter the string\n");
gets(s2);
while(s2[i])
{
s1[i]=s2[i];
i++;
}
s1[i]='\0';
printf("\n%s",s1);
getch();
}

//WRITE A PROGRAMME TO READ AND PRINT A STRING USING SCANF,PRINTF,GETS,PUTS AND %S FORMAT SPECIFIER

#include<conio.h>
#include<stdio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("enter the first string\n");
//use of scanf with %s to read string
scanf("%s",s1);
printf("%s",s1);

printf("enter the second string\n");
//use of gets and puts to read and display string
fflush(stdin);
gets(s2);
puts(s2);

getch();
}

Wednesday, May 14, 2014

WRITE A PROGRAMME TO INSERT AN ELEMENT 1-AT THE BEGINNING OF AN ARRAY 2-AT THE END OF AN ARRAY 3-AT A PARTICULAR POSITION IN AN ARRAY

/*WRITE A PROGRAMME TO INSERT AN ELEMENT
1-AT THE BEGINNING OF AN ARRAY
2-AT THE END OF AN ARRAY
3-AT A PARTICULAR POSITION IN AN ARRAY
*/

#include<conio.h>
#include<stdio.h>
void main()
{
int ar[10],i,choice,item,n,pos;
clrscr();
printf("enter the limit of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("enter the element to be inserted\n");
scanf("%d",&item);
printf("enter the choice\n");
scanf("%d",&choice);
printf("1=INSERT AT THE BEGINING\n2=INSERT AT THE END\n3=INSERT AT PARTICULAR LOCATION\n");
switch(choice)
{
case 1:
for(i=n;i>0;i--)
{
ar[i]=ar[i-1];
}
ar[i]=item;
break;
case 2:
ar[n]=item;
break;
case 3:
printf("enter the position\n");
scanf("%d",&pos);
pos--;
for(i=n;i>pos;i--)
{
ar[i]=ar[i-1];
}
ar[i]=item;
break;
default:
printf("wrong choice");
}
printf("now the elments of array are\n");
for(i=0;i<=n;i++)
{
printf("%d",ar[i]);
}
getch();
}