Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1....
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int i,flag=0,count=1;
- char str[20],ch;
- int pos[27];
- printf("Enter any string : ");
- gets(str);
- for(int i=0;i<26;i++)
- {
- pos[i]=-1;
- }
- int cnt=1;
- for(int i=0;str[i]!='\0';i++)
- {
- if(str[i]==' ')
- continue;
- else
- {
- int c=str[i]-'a';
- if(pos[c]==-1)
- pos[c]=cnt;
- cnt++;
- }
- }
- printf("\n Enter character to be searched :");
- scanf("%c",&ch);
- int x=ch-'a';
- if(pos[x]!=-1)
- {
- printf("position of %c in the string is :%d\n",ch,pos[x]);
- }
- else
- {
- printf("Not found");
- }
- return 0;
- }
- .............................
- 2...
- #include<stdio.h>
- #include<math.h>
- #include<stdlib.h>
- struct point
- {
- int x,y;
- };
- int main()
- {
- point P,Q;
- printf("Enter the x,y coordinates of first location \n");
- scanf("%d %d",&P.x,&P.y);
- printf("Enter the x,y coordinates of second location \n");
- scanf("%d %d",&Q.x,&Q.y);
- int manhattandis=abs(P.x-Q.x)+abs(P.y-Q.y);
- printf("The Manhattan distance between them %d\n",manhattandis);
- return 0;
- }
- 3...............
- #include<stdio.h>
- int main()
- {
- int arr[100];
- int n=15;
- int *ptr=arr;
- for(int i=0;i<n;i++)
- {
- scanf("%d",ptr);
- ptr++;
- }
- ptr=arr;
- for(int i=0;i<n;i++)
- {
- printf("%d ",*ptr);
- ptr++;
- }
- }
- ..................
- 4...
- #include<stdio.h>
- struct student
- {
- char name[30];
- int id;
- char dept[20];
- float cgpa;
- };
- int main()
- {
- student S[3];
- FILE *fp;
- char str[100];
- fp=fopen("student.txt","r");
- if(fp==NULL)
- {
- printf("Error opening file");
- return 0;
- }
- while(fgets(str,100,fp)!=NULL)
- {
- printf("%s\n",str);
- }
- fclose(fp);
- return 0;
- }
- ..........
- 5.
- #include <stdio.h>
- #define N 100
- void transposematrix(int arr[N][N])
- {
- int row,col;
- printf("Enter the number of Row\n");
- scanf("%d",&row);
- printf("Enter the number of column\n");
- scanf("%d",&col);
- printf("Enter the numbers\n");
- for(int i=0;i<row;i++)
- {
- for(int j=0;j<col;j++)
- {
- scanf("%d",&arr[i][j]);
- }
- }
- printf("The Transpose Matrix is:\n");
- for(int i=0;i<col;i++)
- {
- for(int j=0;j<row;j++)
- {
- printf("%d ",arr[j][i]);
- }
- printf("\n");
- }
- }
- int main()
- {
- int arr[N][N];
- transposematrix(arr);
- return 0;
- }
- 6..........
- #include <stdio.h>
- #define N 100
- void mirrormatrix(int arr[N][N])
- {
- int row,col;
- printf("Enter the number of Row\n");
- scanf("%d",&row);
- printf("Enter the number of column\n");
- scanf("%d",&col);
- for(int i=0;i<row;i++)
- {
- for(int j=0;j<col;j++)
- {
- scanf("%d",&arr[i][j]);
- }
- }
- printf("The Mirror Matrix is:\n");
- for(int i=0;i<row;i++)
- {
- for(int j=col-1;j>=0;j--)
- {
- printf("%d ",arr[i][j]);
- }
- printf("\n");
- }
- }
- int main()
- {
- int arr[N][N];
- mirrormatrix(arr);
- return 0;
- }
- 7.
- #include <stdio.h>
- #define MAX_SIZE 100 // Maximum string size
- #define MAX_CHARS 255 // Maximum characters allowed
- int main()
- {
- char str[MAX_SIZE];
- int freq[MAX_CHARS]; // Store frequency of each character
- int i = 0, max;
- int ascii;
- printf("Enter any string: ");
- gets(str);
- /* Initializes frequency of all characters to 0 */
- for(i=0; i<MAX_CHARS; i++)
- {
- freq[i] = 0;
- }
- /* Finds frequency of each characters */
- i=0;
- while(str[i] != '\0')
- {
- ascii = (int)str[i];
- freq[ascii] += 1;
- i++;
- }
- /* Finds maximum frequency */
- max = 0;
- for(i=0; i<MAX_CHARS; i++)
- {
- if(freq[i] > freq[max])
- max = i;
- }
- printf("Maximum occurring character is '%c' = %d times.", max, freq[max]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement