sort the given string, without using string handling functions

30
Sort the given string, without using string handling functions

Upload: maylin

Post on 08-Jan-2016

28 views

Category:

Documents


1 download

DESCRIPTION

Sort the given string, without using string handling functions. More Questions. #include< conio.h > #include< stdio.h > void main() { char s[20],t; int i,j,n ; clrscr (); printf ("Enter a string "); scanf ("% s",s ); printf ("given string % s",s ); for( i =0;s[ i ]!='\0';i++) { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Sort the given string, without using string handling functions

Sort the given string, without using string handling functions

Page 2: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• char s[20],t;• int i,j,n;• clrscr();• printf("Enter a string ");• scanf("%s",s);• printf("given string %s",s);• for(i=0;s[i]!='\0';i++)• {• for(j=i+1;s[j]!='\0';j++)• {• if(s[i]>s[j])• {• t=s[i];• s[i]=s[j];• s[j]=t;• }• }• }• printf("\nsorted string %s",s);• getch();• }

popo

Page 3: Sort the given string, without using string handling functions

Sort each words in the given paragraph

Page 4: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• char s[20],temp[20][20],t;• int i,j,n=0,k=0,l=0;• clrscr();• printf("Enter a string ");• gets(s);• printf("given string %s",s);• for(i=0;s[i]!='\0';i++){l++;}• for(i=0;i<=l;i++)• {• if(s[i]==' '||s[i]=='\0')• {• temp[n][k]='\0';• //printf("\n%s",temp[n]);• n++;k=0;• }• else• {• temp[n][k]=s[i];• k++;• }• }•

popo

Page 5: Sort the given string, without using string handling functions

More Questions• for(i=0;i<n;i++)• {• for(j=0;temp[i][j]!='\0';j++)• {• for(k=j+1;temp[i][k]!='\0';k++)• {• if(temp[i][j]>temp[i][k])• {• t=temp[i][j];• temp[i][j]=temp[i][k];• temp[i][k]=t;• }• }• }• }• k=0;• for(i=0;i<n;i++)• {• for(j=0;temp[i][j]!='\0';j++)• {• s[k]=temp[i][j];• k++;• }• s[k]=' ';k++;• }• s[k]='\0';• printf("\nfinal string : %s",s);• getch();• }

popo

Page 6: Sort the given string, without using string handling functions

Compare two strings, the length of each stings will be calculate using function and the function return the length,(without using string

handling functions)

Page 7: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• int strlength(char *);• char s1[20],s2[20];• int i,j,e=0,b=0,s=0;• clrscr();• printf("enter two strings :- ");• scanf("%s%s",s1,s2);• if(strlength(s1)==strlength(s2))• {• for(i=0;i<strlength(s1);i++)• {• if(s1[i]==s2[i])• {• e++;• }• if(s1[i]<s2[i])• {• s=1; break;• }•

popo

Page 8: Sort the given string, without using string handling functions

More Questions• if(s1[i]>s2[i])• {• b=1; break;• }• }• }• if(e==strlength(s1)){printf("\nTwo strings are Equal");}• if(b==1){printf("\nFirst string is Biggest");}• if(s==1){printf("\nSecond string is Biggest");}• getch();• }• int strlength(char *s)• {• int i,l=0;• for(i=0;s[i]!='\0';i++)• {• l++;• }• return(l);• }

popo

Page 9: Sort the given string, without using string handling functions

Sort N Strings, without using string functions

Page 10: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• int strcompare(char *,char *);• char s[20][20],t[20];• int i,j,n,k;• clrscr();• printf("enter limit ");• scanf("%d",&n);• printf("Enter strings ");• for(i=0;i<n;i++)• {• scanf("%s",s[i]);• }• printf("Given Strings ");• for(i=0;i<n;i++)• {• printf("\n%s",s[i]);• }•

popo

Page 11: Sort the given string, without using string handling functions

More Questions• for(i=0;i<n;i++)• {• for(j=i+1;j<n;j++)• {• if(strcompare(s[i],s[j])==1)• {• for(k=0;s[i][k]!='\0';k++)• {• t[k]=s[i][k];• }• t[k]='\0';• for(k=0;s[j][k]!='\0';k++)• {• s[i][k]=s[j][k];• }• s[i][k]='\0';• for(k=0;t[k]!='\0';k++)• {• s[j][k]=t[k];• }• s[j][k]='\0';• }• }• }•

popo

Page 12: Sort the given string, without using string handling functions

More Questions• printf("\nsorted Strings ");• for(i=0;i<n;i++)• {• printf("\n%s",s[i]);• }• getch();• }• int strcompare(char *s1,char *s2)• {• int strlength(char *);• int i,e=0,b=0,s=0;• for(i=0;i<strlength(s1);i++)• {• if(s1[i]==s2[i])• {• e++;• }• if(s1[i]<s2[i])• {• s=1; break;• }

popo

Page 13: Sort the given string, without using string handling functions

More Questions• if(s1[i]>s2[i])• {• b=1; break;• }• }• if(e==strlength(s1)){return(e);}• if(b==1){return(1);}• if(s==1){return(-1);}

• }• int strlength(char *s)• {• int i,l=0;• for(i=0;s[i]!='\0';i++)• {• l++;• }• return(l);• }

popo

Page 14: Sort the given string, without using string handling functions

Write a function which receives 2 integer arrays with limit n, add

each element of the 2 arrays and the function return the resultant

array

Page 15: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• int *array(int [],int [],int);• int a[10],b[10],i,n,*p;• clrscr();• printf("enter limit ");• scanf("%d",&n);• printf("enter first array ");• for(i=0;i<n;i++)• {• scanf("%d",&a[i]);• }• printf("enter second array ");• for(i=0;i<n;i++)• {• scanf("%d",&b[i]);• }•

popo

Page 16: Sort the given string, without using string handling functions

More Questions• printf("resultant array ");• for(i=0;i<n;i++)• {• printf("%d ",*p);p++;• }• getch();• }• int *array(int a[10],int b[10],int n)• {• int i;• for(i=0;i<n;i++)• {• a[i]=a[i]+b[i];• }• return(a);• }

popo

Page 17: Sort the given string, without using string handling functions

Write a C program using pointers to find the sum of all elements sorted

in an array

Page 18: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• int a[10],i,n,j,t,s=0;• clrscr();• printf("enter limit ");• scanf("%d",&n);• printf("enter array ");• for(i=0;i<n;i++)• {• scanf("%d",(a+i));• }• printf("given array ");• for(i=0;i<n;i++)• {• printf("%d ",*(a+i));• }•

popo

Page 19: Sort the given string, without using string handling functions

More Questions• //sorting• for(i=0;i<n;i++)• {• for(j=i+1;j<n;j++)• {• if(*(a+i)>*(a+j))• {• t=*(a+i);• *(a+i)=*(a+j);• *(a+j)=t;• }• }• }• printf("\nsorted array ");• for(i=0;i<n;i++)• {• printf("%d ",*(a+i));• s=s+*(a+i);• }• printf("\nsum= %d",s);• getch();• }

popo

Page 20: Sort the given string, without using string handling functions

Using an array of pointers to store address of n variables and sort the

array

Page 21: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• int a[10],i,n,*ptr[10],*t,j;• clrscr();• printf("Enter limit ");• scanf("%d",&n);• //sorting address to array of pointers• for(i=0;i<n;i++){ptr[i]=&a[i];}• printf("Enter array ");• for(i=0;i<n;i++)• {• scanf("%d",ptr[i]);• }• printf("\ngiven array ");• for(i=0;i<n;i++)• {• printf("%d ",*ptr[i]);• }•

popo

Page 22: Sort the given string, without using string handling functions

More Questions• //sorting• for(i=0;i<n;i++)• {• for(j=i+1;j<n;j++)• {• if(*ptr[i]>*ptr[j])• {• t=ptr[i];• ptr[i]=ptr[j];• ptr[j]=t;• }• }• }• printf("\nsorted array ");• for(i=0;i<n;i++)• {• printf("%d ",*ptr[i]);• }• getch();• }

popo

Page 23: Sort the given string, without using string handling functions

Write a C program that receives one command line argument,

which is a file name. open this file in the main and read an array of all

integer values

Page 24: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main(int argc ,char *argv[])• {• FILE *fp;• int a[10],i,n=0;• clrscr();• fp=fopen(argv[1],"r");• while((a[n]=getw(fp))!=EOF)• {• n++;• }• fclose(fp);• printf("\ncontent in the array ");• for(i=0;i<n;i++)• {• printf("%d ",a[i]);• }• getch();• }

popo

Page 25: Sort the given string, without using string handling functions

Write a C program that read a 2D array and prints whether it stores a

unit matrix or not

Page 26: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main()• {• int a[10][10],i,j,n,m,f=1;• clrscr();• printf("enter order of the matrix");• scanf("%d%d",&n,&m);• if(n==m)• {• printf("enter matrix");• for(i=0;i<n;i++)• {• for(j=0;j<m;j++)• {• scanf("%d",&a[i][j]);• }• }•

popo

Page 27: Sort the given string, without using string handling functions

More Questions• printf("\ngiven matrix\n");• for(i=0;i<n;i++)• {• for(j=0;j<m;j++)• {• printf("%d ",a[i][j]);• if(i==j)• {• if(a[i][j]!=1)• {• f=0;• }• }• else• {• if(a[i][j]!=0)• {• f=0;• }• }• }• printf("\n");• }

popo

Page 28: Sort the given string, without using string handling functions

More Questions• if(f==1)• {• printf("\ngiven matrix is unit matrix");• }• else• {• printf("\ngiven matrix not unit matrix");• }• }• else• {• printf("\nNot a square matrix");• }• getch();• }

popo

Page 29: Sort the given string, without using string handling functions

Write a program that will receive a filename and a text as command

line argument and write the text to the file

Page 30: Sort the given string, without using string handling functions

More Questions• #include<conio.h>• #include<stdio.h>• void main(int argc,char *argv[])• {• FILE *fp;• char c;• fp=fopen(argv[1],"w");• fprintf(fp,"%s",argv[2]);• fclose(fp);• fp=fopen(argv[1],"r");• printf("\ncontent of the file %s",argv[1]);• while((c=getc(fp))!=EOF)• {• printf("%c",c);• }• fclose(fp);• getch();• }

popo