9.4.20

string sorting


STRING SORTING
#include<stdio.h>
#include<strings.h>
int main()
{

    char st[10][30],t[30];
    int n,i,j,flag;
    printf("How many strings do u want to enter :");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {

    fflush(stdin);
    gets(st[i]);

    }
for(i=0;i<n-1;i++)
{

    flag=1;
    for(j=0;j<n-1-i;j++)
{
    if(strcmp(st[j],st[j+1])>0)
    {

        strcpy(t,st[j]);
        strcpy(st[j],st[j+1]);
        strcpy(st[j+1],t);
        flag=0;
    }
}
if(flag==1)
    break;
}
printf("In alphabetical order\n");
for(i=0;i<n;i++)
    printf("%s\n",st[i]);
return 0;
}
OUTPUT
How many strings do u want to enter 4
Sirisha
Dali
Deepthi
Rama
In alphabetical order
Dali
Deepthi
Rama
Sirisha

finding the string which occurs first in alphabetical order without modifying the array


FINDING THE STRING WHICH OCCURS FIRST IN ALPHABTICAL ORDER WITHOUT MODIFYING THE ARRAY
#include <stdio.h>
void main()
{

    char st[10][30],t[30];
    int n,i;
    printf("how many strings do u want to enter :");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {

        fflush(stdin);
        gets(st[i]);

    }
for(i=0;i<n;i++)
    if(i==0||strcmp(t,st[i]>0))2

strcpy(t,st[i]);
printf("The string which occurs first in alphabetical order is %s",t);
}
OUPUT
How many strings do u want to enter :1
Enter strings: abcde
The string which occurs first in alphabetical order is abcde

finding the longest string


FINDING THE LONGEST STRING
#include <stdio.h>
void main()
{

    char st[10][30],t[30];
    int n,i,l,max=0;
    printf("How many strings do u want ot enter:");
    scanf("%d",&n);
    printf("enter strings:");
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        gets(st[i]);
        l=strlen(st[i]);
        if(l>max)
        {
            max=1;
            strcpy(t,st[i]);

        }
    }
    printf("Longest string is : %s",t);
    }
OUTPUT
How many strings do u want to enter 2
Enter strings: sirisha
Sanapathi
Longest string sanapathi

reading and printing a set of strings


READING /PRINTING A SET OF STRINGS
#include <stdio.h>
void main()
{

    char st[10][30];
    int n,i;
    printf("how many strings do u want to enter:");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        gets(st[i]);
    }
printf("The entered strings are\n");
for(i=0;i<n;i++)
printf("%s\n",st[i]);
}
OUTPUT
How mant strings do u want to enter : 5
Enter strings:Dwarakanagar
MVP
Gajuwaka
Scindia
NAD
The entered strings are : Dwarakanagar
MVP
Gajuwaka
Scindia
NAD

transpose matrix


TRANSPOSE MATRIX
#include <stdio.h>
void main()
{

    int m[5][5],t[5][5],i,j,r,c;
    printf("How many rows and cols");
    scanf("%d%d",&r,&c);
    printf("enter the matrix:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m[i][j]);
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        t[j][i]=m[i][j];
    printf("original matrix:\n");
    for(i=0;i<r;i++)
    {

        for(j=0;j<c;j++)
            printf("%5d",m[i][j]);
        printf("\n");

    }
printf("\n its transpose:\n");
for(i=0;i<c;i++)
{

    for(j=0;j<r;j++)
        printf("%5d",t[i][j]);
    printf("\n");
}
}
OUTPUT
How many rows and cols 2
2
Enter the matrix: 1
2
3
4
Orginal matrix:
1               2
2               4
Its transpose
1                 3
2                 4

product of two matrices


PRODUCT OF TWO MATRICES
#include <stdio.h>
void main()
{

    int m1[5][5],m2[5][5],m3[5][5]={0},r1,r2,c1,c2,i,j,k;
    printf("how many rows and cols for matrix 1");
    scanf("%d%d",&r1,&c1);
    printf("how many rows and cols for matrix 2");
    scanf("%d%d",&r2,&c2);
    if(c1!=r2)
    {

        printf("Multiplication rule violated -cannot be multiplied");
        exit(0);
    }

    printf("Enter elements into matrix 1:");
    for(i=0;i<r1;i++)
        for(j=0;j<c1;j++)
        scanf("%d",&m1[i][j]);

printf("Enter elements into matrix 2:");
    for(i=0;i<r2;i++)
        for(j=0;j<c2;j++)
        scanf("%d",&m2[i][j]);
        for(i=0;i<r1;i++)
            {


            for(j=0;j<c2;j++)
            {
                for(k=0;k<c1;k++)
                    m3[i][j]=m3[i][j]+m1[i][j]*m2[i][j];
            }
}
        printf("Product of two matrices:\n");
        for(i=0;i<r1;i++)
        {

            for(j=0;j<c2;j++)
                printf("%5d",m3[i][j]);
            printf("\n");




        }

}
OUTPUT
How many rows and cols for matrix 1 2
2
How may rows and cols for matrix 2  2
2
Enter element into matrix 1:1
2
3
4

Enter element into matrix 2:1
2
3
4

Product of two matrices:
2         8
18    32

sum of two matrices


SUM OF TWO MATRICES
#include <stdio.h>
void main()
{

    int m1[5][5],m2[5][5],m3[5][5],r,c,i,j;
    printf("how many rows and cols ");
    scanf("%d%d",&r,&c);
    printf("Enter elements into matrix 1:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m1[i][j]);

printf("Enter elements into matrix 2:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m2[i][j]);
        for(i=0;i<r;i++)
            for(j=0;j<c;j++)
            m3[i][j]=m1[i][j]+m2[i][j];
        printf("sum of two matrices:\n");
        for(i=0;i<r;i++)
        {

            for(j=0;j<c;j++)
                printf("%3d",m3[i][j]);
            printf("\n");



           
        }

}
OUTPUT
How many rows and cols 2
2
Enter elements into matrix 1:1
2
3
4
Enter elements into matrix 2:1
2
3
4
Sum of matrices:
2  4
6   8

sum of elements of a matrix


SUM OF ELEMENTS OF A MATRIX
#include <stdio.h>
void main()
{

    int m[5][5],r,c,i,j,s=0;
    printf("How many rows and cols");
    scanf("%d%d",&r,&c);
    printf("enter elements");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
    {
        scanf("%d",&m[i][j]);
        s=s+m[i][j];
    }
printf("sum of elements =%d",s);
    22
   
}
OUTPUT
How many rows and cols 2
4
Enter elements 1
2
3
4
5
6
7
8
Sum of elements 36

reading and printing a matrix


READING AND PRINTING A MATRIX
#include <stdio.h>
int main()
{

    int m[5][5],r,c,i,j;
    printf("How many rows and cols ");
    scanf("%d%d",&r,&c);
    printf("Enter elements");
    for(i=0;i<r;i++)
       for(j=0;j<c;j++)
        scanf("%d",&m[i][j]);

    printf(" The entered elements are \n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
       printf("%3d",m[i][j]);
       printf("\n");

    }
return 0;
}
OUTPUT
How many rows and cols 2 3
Enter elements 1
1
3
5
6
7
The enetered elements are
113
567

finding number of words in a sentence example 2


FINDING NUMBER OF WORDS IN A SENTENCE EXAMPLE 2
#include <stdio.h>
void main()
{

    char st[128];
    int i,nwords=0;
    puts("enter a sentence:");
    gets(st);
    for(i=0;st[i]!=0;i++)
        if(st[i]==' ' && st[i+1]!=' ')
        nwords++;
    printf("number of words=%d",nwords+1);

}
Output:
Enter a sentence
Incredible india
Number of words =2

finding number of word in a sentence example 1


FINDING NUMBER OF WORDS IN A SENTENCE EXAMPLE 1
#include <stdio.h>
int main()
{
    char st[128];
    int i,nwords=0;
    puts("enter a sentence");
    gets(st);
    for(i=0;st[i]!=0;i++)
         if(st[i]=='')
        nwords++;
    printf("numbers of words=%d",nwords+1);
return 0;
}
OUTPUT
Enter a sentence
Incredible india
Number of words =2


string concatenation without using strcat()

STRING CONCATENATION WITHOUT USING STRCAT()
#include <stdio.h>
void main()
{

    char st1[30],st2[30],st3[60];
    int i,j;
    puts("enter a string 1");
    gets(st1);
    puts("enter a string 2");
    gets(st2);
    for(i=0;st1[i]!=0;i++)
        st3[i]=st1[i];
    st3[i]='*';
    j=i+1;
    for(i=0;st2[i]!=0;i++,j++)
        st3[j]=st2[i];
    st3[j]=0;
    printf("%s\n%s\n%s",st1,st2,st3);

}
OUTPUT
Enter a string 1 siri
Enter a string 2 sha
Siri
Sha
Siri*sha

string concatenation wihtout using strcat()


STRING CONCATENATION WITHOUT USING STRCAT()
#include <stdio.h>
int main()
{
    char st1[30],st2[30];
    int l1,l2,i;
    puts("enter string 1:");
    gets(st1);
    puts("enter string 2:");
    gets(st2);
    for(l1=0;st1[l1]!=0;l1++)
        for(l2=0;st1[l2]!=0;l2++)
    if(l1!=l2)
    {

        printf("not same");
        exit(0);

    }
    for(i=0;i<l1;i++)
        if(st1[i]!=st2[i])
        break;
    if(i==l1)
        printf("same");
    else
        printf("not same");
return 0;
}
OUTPUT
               Enter a string 1: siri
Enter a string 2: sana
Not same


string reverse without using strrev()


STRING REVERSE WITHOUT USING STRREV()
#include <stdio.h>
void main()
{

char st1[30],st2[30];
int l,i,j;
puts("enter a string");
gets(st1);
for(l=0;st1[l]!=0;l++)
j=l-1;
for(i=0;i<l;i++,j--)
    st2[i]=st1[j];
st2[i]=0;
printf("original : %s\nReverse:%s",st1,st2);
}
OUTPUT
Enter a string sirisha
Original sirisha
Reverse ahsiris

string copy without using strcpy()


STRING COPY WITHOUT USING STRCPY()
#include<stdio.h>
#include<stdlib.h>

int main()
{
    char st1[30],st2[30];
    int i;
    puts("Enter a string");
    gets(st1);
    for(i=0;st1[i]!='\0';i++)
        st2[i]=st1[i];
    st2[i]='\0';
    printf("%s\n%s",st1,st2);
return 0;
}
OUTPUT
Enter a string sirisha
Sirisha
Sirisha

string length wihtout using strlen()


STRING LENGTH WITHOUT USING STRLEN()
#include <stdio.h>
void main()
{

    char st[30];
    int i;
    puts("Enter a string");
    gets(st);
    for(i=0;st[i]!='\0';i++);sirisha
    printf("Length %d",i);

}
OUTPUT
Enter a string :sirisha sanapathi
Length:17

string concatenation


STRING CONCATENATION
#include <stdio.h>
void main()
{

    char st1[30],st2[30],st3[60];
    int n;
    printf("enter atring 1:");
    gets(st1);
    printf("enter string 2");
    gets(st2);
    strcpy(st3,st2);
strcat(st3,st2);
    printf(" %s\n%s\n%s",st1,st2,st3);
}
OUTPUT
Enter string1: shiva
Enter string 2: rama
Shiva
Rama
Shivarama          


string comparison


STRING COMPARISON
#include <stdio.h>
void main()
{

    char st1[30],st2[30];
    int n;
    printf("enter atring 1:");
    gets(st1);
    printf("enter string 2");
    gets(st2);
n=strcmp(st1,st2);
if(n==0)
    printf("same");
else
    printf("not same");
}
OUTPUT:
Enter string 1: sachin
Enter string 2:SAchin
Not same

string reverse


STRING REVERSE
#include <stdio.h>
void main()
{

    char st1[30],st2[30];
    printf("enter a string");
    gets(st1);
    strcpy(st2,st1);
    strrev(st2);
    printf("orginal:%s\nReverse:%s",st1,st2);
}
OUTPUT:
Enter a string: Visakhapatnam
Original:Visakhapatnam
Reverse :mantapahkasiv

string copy


STRING COPY
#include <stdio.h>
void main()
{

    char st1[30],st2[30];
    printf("enter a string ");
    gets(st1);
    strcpy(st2,st1);
    printf("%s\n%s",st1,st2);in
}
OUTPUT
Enter a string : india
India
India

finding a string length


FINDING A STRING LENGTH
#include <stdio.h>
void main()
{
    char st[30];
    int l;
    puts("enter a string");
    gets(st);
    l=strlen(st);
    printf("Length=%d",l);
}
OUTPUT
Enter a string sirisha sanapathi
Length =16

reading multi word names using gets()


READING MULTI-WORD NAMES USING gets()
#include <stdio.h>
void main()
{

    char na[30];
    printf("enter a name");
    gets(na);
    printf("%s",na);
}
OUTPUT
Enter a name sirisha sanapathi
Sirisha sanapathi

reading and printing a group of characters (method 1)(fflush())


READING/PRINTING A GROUP OF CHARACTERS (METHOD 1)(fflush())
#include <stdio.h>
void main()
{

    char st[30];
    int n,i;
    printf("How many characters do u want to enter");
    scanf("%d",&n);
    printf("enter the characters(name):");
    fflush(stdin);
    for(i=0;i<n;i++)
        scanf("%c",&st[i]);
    printf("the entered name is :");
    for(i=0;i<n;i++)
        printf("%c",st[i]);
}
OUTPUT
How many characters do u want to enter 13
Enter the character(name):incredible india
The entered name is :incredible in
READING/PRINTING A GROUP OF CHARACTERS (METHOD 2)
#include <stdio.h>
void main()
{

    char st[30];
    printf("enter the characters(name)");
    scanf("%s",st);
    printf(" the entered name is %s",st);

}
OUTPUT
Enter the characters(name) sirisha sanapathi
The entered name is sirisha

array elements in ascending order


Ascending order array elements
#include <stdio.h>
void main()
{

    int a[10],n,i,j,t,flag;
    printf("How many numbers do u want to enter");
    scanf("%d",&n);
    printf("enter the numbers");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n-1;i++)
    {

        flag=1;
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
                flag=0;

            }
        }
        if(flag==1)
            break;

    } printf("in ascending order\n");
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);
}
Output
How many numbers do u want enter 5
Enter the numbers:2
8
6
3
1
In ascending order
1
2
3
6
8


results sheet


Results sheet:
#include <stdio.h>
#include <stdlib.h>

void main()
{

    int rno[10],marks[10],n,i;
    printf("How many students details do u want to enter ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter id and marks:");
        scanf("%d%d",&rno[i],&marks[i]);
    }
printf("Results sheet\n");
printf("--------------\n\n");
printf("First class \n");
for(i=0;i<n;i++)
    if(marks[i]>=60)
    printf("%d->%d\n",rno[i],marks[i]);
printf("Second class\n");
for(i=0;i<n;i++)
    if(marks[i]>=50&&marks[i]<60)
    printf("%d->%d\n",rno[i],marks[i]);
printf("Third  class\n");
for(i=0;i<n;i++)
if(marks[i]>=35&&marks[i]<50)
    printf("%d->%d\n",rno[i],marks[i]);

}
Output:
How many students details do u want to enter 2
Enter id and marks: 1
36
Enter id and marks:2
56
Results sheet
-----------------
First class
Second class
2->56
Third class
1->35

Sum of array


Sum of array :
#include <stdio.h>
void main()
{

    int a[10],n,i,s=0;
    printf("How many numbers do u want to enter");
    scanf("%d",&n);
    printf("enter numbers");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        s=s+a[i];
    }
    printf("sum =%d",s);
}
Output:
How many numbers do u want to enter 5
20
10
50
22
90
Sum=192

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...