1 / 30

Sort the given string, without using string handling functions

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 ]!='';i++) {

maylin
Download Presentation

Sort the given string, without using string handling functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sort the given string, without using string handling functions

  2. More Questions • #include<conio.h> • #include<stdio.h> • void main() • { • char s[20],t; • inti,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

  3. Sort each words in the given paragraph

  4. More Questions • #include<conio.h> • #include<stdio.h> • void main() • { • char s[20],temp[20][20],t; • inti,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

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

  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<conio.h> • #include<stdio.h> • void main() • { • intstrlength(char *); • char s1[20],s2[20]; • inti,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

  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(); • } • intstrlength(char *s) • { • inti,l=0; • for(i=0;s[i]!='\0';i++) • { • l++; • } • return(l); • } • popo

  9. Sort N Strings, without using string functions

  10. More Questions • #include<conio.h> • #include<stdio.h> • void main() • { • intstrcompare(char *,char *); • char s[20][20],t[20]; • inti,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

  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'; • } • } • } • popo

  12. More Questions • printf("\nsorted Strings "); • for(i=0;i<n;i++) • { • printf("\n%s",s[i]); • } • getch(); • } • intstrcompare(char *s1,char *s2) • { • intstrlength(char *); • inti,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

  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);} • } • intstrlength(char *s) • { • inti,l=0; • for(i=0;s[i]!='\0';i++) • { • l++; • } • return(l); • } • popo

  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<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

  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) • { • inti; • for(i=0;i<n;i++) • { • a[i]=a[i]+b[i]; • } • return(a); • } • popo

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

  18. 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

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

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

  21. 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

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

  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<conio.h> • #include<stdio.h> • void main(intargc ,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

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

  26. 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

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

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

  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<conio.h> • #include<stdio.h> • void main(intargc,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

More Related