Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sort the given string, without using string handling functions.

Similar presentations


Presentation on theme: "Sort the given string, without using string handling functions."— Presentation transcript:

1 Sort the given string, without using string handling functions

2 More Questions #include 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(); }

3 Sort each words in the given paragraph

4 More Questions #include 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++; }

5 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(); }

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

7 More Questions #include 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; }

8 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); }

9 Sort N Strings, without using string functions

10 More Questions #include 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]); }

11 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'; }

12 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; }

13 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); }

14 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

15 More Questions #include 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]); }

16 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); }

17 Write a C program using pointers to find the sum of all elements sorted in an array

18 More Questions #include 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)); }

19 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(); }

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

21 More Questions #include 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]); }

22 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(); }

23 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

24 More Questions #include 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(); }

25 Write a C program that read a 2D array and prints whether it stores a unit matrix or not

26 More Questions #include 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]); }

27 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"); }

28 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(); }

29 Write a program that will receive a filename and a text as command line argument and write the text to the file

30 More Questions #include 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(); }


Download ppt "Sort the given string, without using string handling functions."

Similar presentations


Ads by Google