Friday, 20 July 2018

Alphabets Plus Digits Sum

The program must accept a string S which has alphabets and digits as the input. The program must find the sum of all digits as D in the input string S. Then the program must print the alphabets which are D positions away from the alphabets in the string S.

Example

Input
435acl

The digits are 4,3,5 and their sum D=12

Output
mox
The alphabets in the input are a, c, l
a+12 = m    c+12 = o    l+12 = x

Program
#include <stdio.h>
#include <ctype.h>

int main()
{
    int D=0,i,c=0;
    char S[100];
    scanf("%s",S);
    for(i=0;S[i]!='\0';i++)
        if (isdigit(S[i]))
        {
            D+=S[i]-48;
        }
    D=D%26;

    for(i=0;S[i]!='\0';i++)
   {
      
        if (isalpha(S[i])) 
       {
           
            S[i]=tolower(S[i]);
            c=D+S[i];
            
            if(c>122)
            {
                c=96+(c-122);
            }
              
            printf("%c",c);
        }
    }   
}

OUTPUT


1121ZU                                                                                                                         
                                                                                                                              
ez  

Friday, 13 July 2018

Vertical ZigZag - C columns

This C program accepts two numbers R and C as input and prints the vertical zigzag C columns.

Example 1

Input
5 5

Output
1  10  11  20  21
2   9   12  19  22
3   8   13  18  23
4   7   14  17  24
5   6   15  16  25

Example 2
Input
2 3

Output
1 4 5
2 3 6

Program

#include <stdio.h>

int main()
{
   int a[10][10],R,C,i,j,k=1;
   scanf("%d%d",&R,&C);
    for(i=1;i<=C;i++)
   {
        for(j=1;j<=R;j++)
        {
           if  (i%2==1)
             a[i][j]=k++;
           else
             a[i][j]=--k;
    
        }
        k=k+R;
    }
    for(i=1;i<=R;i++)
    {
         for(j=1;j<=C;j++)
           printf("%d ",a[j][i]);
         printf("\n");
    }
    
}

OUTPUT
5 5

1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25



Wednesday, 6 June 2018

C program to print a number if next number has the same unit digit

Next Number Same Unit Digit

An array of N integers are given as input. The program must print the integers only if the unit digit
of the current integer and the unit digit of the next integer are same

Boundary Condition
2 <= N <= 1000

Program
***********

#include <stdio.h>

int main()
{
    int N,a[1000],i,j;

    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%d",&a[i]);
    }    
    if (N>=2 && N<=1000)
    {
        for(i=0;i<N-1;i++)
        {
            j=i+1;
           if((a[i]%10)==(a[j]%10))
           {
               printf("%d\t",a[i]);
           }    
        
        }
    }
        return 0;
}

Example
Input
6
12 834 94 485 285 905

Output
834 485 285

Tuesday, 5 June 2018

Program to count the number of alphabets occurring in all the given N strings

Common Alphabets in N strings

N strings are passed as inputs to the program. Each string will contain only the alphabets a-z in lower case. A given alphabet may be repeated any number of times. The program must print the count C of the alphabets that are present in all the N string values.

Boundary conditions:
2 <= N <= 500
Maximum length of each string = 1000

Program:

********
#include <stdio.h>

#include <string.h>


int main()
{
    char a[500][1000],test[500];
    int i,N,count=0,j,k,len;
    
    scanf("%d",&N);
    if (N>=2 && N<=500)
    {
        for(i=0;i<N;i++)
            scanf("%s",a[i]);
        i=0; j=0;
        len=strlen(a[0]);
       
        while (a[0][i]!='\0')
        {
            for(k=0;k<j;k++)
            {
                if (a[0][i]!=test[k])
                    continue;
                else
                    break;
            }
            if (k>=j)
                test[j++]=a[0][i];
            ++i;
        }
        test[j]='\0';
        printf("%s",test);
        i=0; count=0;
        while (test[i]!='\0')
        {
            for(j=1;j<N;j++)
            {
                if (strchr(a[j],test[i]))
                 continue;
                else
                  break;
            }
            if (j>=N)
                ++count;
            ++i;
        }
        printf("%d",count);
    }
            
   return 0;
}

Example Input/Output:
3
mnppqqr
ajkmnnm
poormanagement

Output:
2

Explanation:
Only two alphabets m and n are present in all the three strings.

Friday, 20 April 2018

Shell script to change content into upper case

#Script to change the file name and contents into upper case
echo "enter the filename"
read fname
echo "File name changed to upper case"
if [ -f $fname ]
then
  echo $fname |tr '[a-z]' '[A-Z]'
fi

echo "\nOriginal file content"

cat $fname
echo "\nFile content in upper case"
tr '[a-z]' '[A-Z]' <$fname



OUTPUT
Safi>sh trans.sh
enter the filename
file2
File name changed to upper case
FILE2

Original file content
103 Banu CIO
103 Banu CIO
103 Banu CIO
104 Shan CFO
101 Sasha MD

File content in upper case
103 BANU CIO
103 BANU CIO
103 BANU CIO
104 SHAN CFO
101 SASHA MD

Thursday, 19 April 2018

Script to reverse the positional parameter list

#shell script to reverse print the positional parameter list
n=$#
for((i=n-1;i>=0;i--))
do
  arr[$i]=$1
  shift
done

echo ${arr[*]}



OUTPUT

Safi>bash revparam.sh 2 4 6 1 5 7 8 9 10
10 9 8 7 5 1 6 4 2

Monday, 9 April 2018

Shell script to find the ncr value

#Script to find the ncr value
echo "enter values of n and r"
read n
read r
t=0 i=1
a=1 b=1
c=1 d=0
while [ $i -le $n ]
do
a=`expr $a \* $i`
i=`expr $i + 1`
done
i=1
while [ $i -le $r ]
do
b=`expr $b \* $i`
i=`expr $i + 1`
done
i=1
d=`expr $n - $r`
while [ $i -le $d ]
do
c=`expr $c \* $i`
i=`expr $i + 1`
done
i=`expr $b \* $c`
t=`expr $a / $i`
echo "result=$t"



OUTPUT
Safi>sh ncr.sh
enter values of n and r
5
3
result=10

Shell script to display file attributes

#Script to extract and display the file attributes
echo "Enter the file name"
read f1
if [ -e $f1 ]
then
ap=`ls -l $f1 | cut -c2-10`
fn=`ls -l $f1 | tr -s " " | cut -d " " -f9`
lmt=`ls -l $f1 | tr -s " " | cut -d " " -f6,7,8`
sof=`ls -l $f1 | tr -s " " | cut -d " " -f5`
nol=`ls -l $f1 | tr -s " " | cut -d " " -f2`
un=`ls -l $f1 | tr -s " " | cut -d " " -f3`
fi
echo "The access permission of the given file is: $ap"
echo "The filename of the given file is: $fn"
echo "The last modification time of the given file is: $lmt"
echo "The size of given file is: $sof"
echo "The number of links of the given file: $nol"
echo "The username of the given file is: $un"



Safi>sh fchar.sh
Enter the file name
fchar.sh
The access permission of the given file is: rw-r--r--
The filename of the given file is: fchar.sh
The last modification time of the given file is: Apr 9 21:43
The size of given file is: 588
The number of links of the given file: 1
The username of the given file is: csc

Shell script to remove multiple copies of the same file content

#Script to compare contents of two files
echo "Enter two file name"
read f1 f2
d=`cmp $f1 $f2`
if [ -z "$d" ]
then
echo "Both file contents are same"
echo "File $f2 is deleted"
rm $f2
else
echo "Both are different"
fi





OUTPUT
Safi>sh cmpdel.sh
Enter two file name
ff file
Both are different

Safi>sh cmpdel.sh
Enter two file name
ff ff
Both file contents are same
File ff is deleted

Shell script to count the number of lines that does not contain a given word

#script to count the lines that does not contain a given word
echo enter file name and word
read file word
line=`grep -v "$word" $file| wc -l`
echo "$line lines does not contain word"



OUTPUT
Safi>sh notpres.sh
enter filename and word
notpres.sh echo
3 lines does not contain word

Shell script to display a multiplication table

#Script to display a multiplication table n times
echo "Enter the value for m and n"
read m n
for((i=1;i<=n;i++))
do
 echo $i '*' $m = `expr $i \* $m`
done



OUTPUT
Safi>bash mult.sh
Enter the value for m and n
5 10
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

Tuesday, 3 April 2018

Shell script to find n!/r!

# shell script to find n!/r!
echo "enter n and r value"
read n r
factn=1
for((i=1; i<=n; i++))
do
    factn=`expr $factn \* $i`
done
factr=1
for((i=1; i<=r; i++))
do
    factr=`expr $factr \* $i`
done
echo "n!/r! = `expr $factn / $factr`"




OUTPUT
Safi> bash nrfact.sh
enter n and r value
5 2
n!/r! = 60

shell script to count the login users and welcome them

#Script to count login users and welcome them
dt=`date +%H`
echo "No. of users currently logged in : `who|cut -d " " -f 1|uniq| wc -l`"
if [ $dt -lt 12 ] ; then
    echo "We welcome you all. Good Morning!"
elif [ $dt -lt 16 ] ; then
    echo "We welcome you all. Good Afternoon!"
else
    echo "We welcome you all. Good Evening!"
fi




OUTPUT
csc@csc-SVE1513CYNB ~ $ sh usrCnt.sh
No. of users currently logged in : 1
We welcome you all. Good Evening!

Monday, 2 April 2018

Shell script to prepare electricity bill

#Script to prepare Electricity Bill
echo "Enter the name"
read name
echo  "Enter the meter number"
read mno
echo "Enter the current month reading"
read cmr
echo "Enter the last month reading"
read lmr
unit=`expr $cmr - $lmr`
if [ $unit -eq 0 ]
then
       charge=40
elif [ $unit -gt 0 -a $unit -le 100 ]
then
      charge=`expr $unit \* 1`
elif [ $unit -gt 100 -a $unit -le 300 ]
then
     unit=`expr $unit - 100`   
     charge=`expr 100 + $unit`
elif [ $unit -gt 300 ]
then
    unit=`expr $unit - 300`   
    charge=`expr $unit \* 2`
    charge=`expr 300 + $charge`
fi
echo "EB Bill"
echo "---------"
echo "Consumer Name: $name"
echo "Meter Number : $mno"
echo "Units Consumed: `expr $cmr - $lmr`"
echo "Amount to be paid: $charge"


OUTPUT
Safi>sh EBbill.sh
Enter the name
ss
Enter the meter number
3340
Enter the current month reading
500
Enter the last month reading
100

EB Bill
---------
Consumer Name: ss
Meter Number : 3340
Units Consumed: 400
Amount to be paid: 500

Shell script to count the vowels in the given string

#script to count the number of vowels in the given string
echo "enter a string"
read str
count=0
len=${#str}
while [ $len -gt 0 ]
do
       ch=`echo "$str" | cut -c $len`
       case $ch in
               [aA] | [eE] | [iI] | [oO] | [uU] ) count=`expr $count + 1`;;
       esac
       len=`expr $len - 1`
done
echo "The number of vowels in the given string '$str' is $count"



OUTPUT
Safi>sh vowcnt.sh
enter a string
Aeroplane
The number of vowels in the given string 'Aeroplane' is 5

Shell script to find m to the power n



echo "Enter the m And n"
read m n
 
#Script to find m power n                                                                                        power=1                                                                                                                                      
for((i=1; i <= n; i++))
do                                                                                                                                                                                                                                                     
          power=`expr $power \* $m`
                                                                                                                       
done                                                                                                                                            
echo "$m power $n = $power"




OUTPUT

csc@csc-SVE1513CYNB ~ $ bash power.sh
Enter the m And n
4 3
4 power 3 = 64

Shell script to display user login time

#Script  to display the time at which specified user logged in
un=$1
t=`who | grep "$un" | head -1 | tr -s " " | cut -d ' ' -f 4`
echo "User $un has logged in at $t time"



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh logTime.sh csc
User 'csc' has logged in at time 02:13



Shell script to perform basic operations on file


#Performs file operations
echo "Enter your choice"
echo "1. Copy 2. Remove 3. Rename 4. Exit"
read choice
if [ $choice -eq 1 ]
then
    echo "Enter the filename to be copied"
    read f1
    echo "enter the target filename"
    read f2
    cp $f1 $f2
fi
if [ $choice -eq 2 ]
then
    echo "enter the filename to be removed"
    read f1
    rm $f1
fi
if [ $choice -eq 3 ]
then
    echo "enter the file name to be renamed"
    read f1
    echo "Enter the file name to replace"
    read f2
    mv $f1 $f2
fi
if [ $choice -eq 4 ]
then
    exit 1
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
1
Enter the filename to be copied
fileOp.sh
enter the target filename
ss
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
2
enter the filename to be removed
ss
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
3
enter the file name to be renamed
fileOp.sh
Enter the file name to replace
fOperations.sh
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
4

Shell script to delete lines containing a pattern


#Deletes lines in a file which contains a given pattern
echo "Enter the pattern to be searched"
read p
n=$#
for i in $*
do
    if [ -f $i ]
        then   
                chk=`grep $p $i`
                if [ -z $chk ]
                then
                       echo "Pattern not found in file '$i'"
               else           
                       `grep -v $p $i > tfile`
                       echo "Before deletion"
                       cat $i
                       echo "After deletion"
                       cp tfile $i
                       cat $i
               fi
        else
               echo "File '$i' does not exist"
        fi
done




OUTPUT
csc@csc-SVE1513CYNB ~ $ sh ss.sh file2 ffff ff
Enter the pattern to be searched
Anu
Before deletion
103 Banu CIO
103 Banu CIO
103 Banu CIO
101 Anu CEO
104 Shan CFO
101 Sasha MD
After deletion
103 Banu CIO
103 Banu CIO
103 Banu CIO
104 Shan CFO
101 Sasha MD
File 'ffff' does not exist
Pattern not found in 'ff'

Friday, 30 March 2018

Shell script to display file contents from middle

#Display file contents between a range
echo "Enter file name"
read fname
echo "Enter start and end line"
read m n

if [ -f $fname ]
then
      tail -n +$m $fname>tfile
      range=`expr $n - $m`
      echo $range
      head -$range tfile

else
      echo "'$fname' does not exist"
fi



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh dispmid.sh
Enter file name
file2
Enter start and end line
2 5
3
102 Babu CEO
103 Banu CIO
101 Anu MD

Shell script to check whether file is executable, readable and writeable

#Check access permission of the given file
echo "Enter file name: \c"
read file
if [ -e $file ]
then
    echo "'$file' is"
    if [ -x $file ]
    then
       echo "   executable"
    fi
    if [ -r $file ]
    then
       echo "    readable"
    fi
    if [ -w $file ]
    then
       echo "    writeable"
    fi
    if [ -r $file -a -w $file ]
    then
       echo "     both readable and writeable"
    fi
else
    echo "'$file' does not exist"
fi




OUTPUT
csc@csc-SVE1513CYNB ~ $ sh ftesh.sh
Enter file name: a.out
'a.out' is
   executable
    readable
    writeable
     both readable and writeable

Shell script to delete files of zero size

#Delete files of size zero in the current directory
for i in *
do
  if [ -f $i -a ! -s $i ]
  then
     
          echo "File $i size is zero"
          rm -i $i
    
   fi
done



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh rmzerofile.sh
File 'hello' size is zero
rm: remove regular empty file ‘hello’? y
File 'well' size is zero
rm: remove regular empty file ‘well’? y


Thursday, 29 March 2018

Shell script to display files of size greater than the specified size

#Display files of size greater than the specified size
echo "Enter size"
read size
for i in *
do
    if [ -f $i ]
    then
          s=`ls -l $i|tr -s ' '|cut -d ' ' -f5`

          if [ $s -gt $size ]
          then
              echo $i
          fi
    fi
done


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh fsize.sh
Enter size
286
Firefox_wallpaper.png
a.out
archive.tar
arrsort.sh
drdl.sh
grp.c
logcnt.sh
occur.sh
oddeven02.sh
palin.sh
present.sh

Shell script to display the number of users logged-in

#Display the number of logged in users and the first logged in user info
count=`who|cut -d ' ' -f1|sort -u|wc -l`
echo "Number of users logged in: $count"

echo "First login user details"
echo "------------------------"
fu=`who|head -n 1|cut -d ' ' -f1`
echo "User name = $fu"
echo "Logged in Terminal = `who|head -n 1|tr -s ' '|cut -d " " -f2`"
echo "Log in date = `who|head -n 1|tr -s ' '|cut -d ' ' -f3`"
echo "Log in time = `who|head -n 1|tr -s ' '|cut -d ' ' -f4`"


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh logcnt.sh
Number of users logged in: 1
First login user details
-------------------------------------
User name = csc
Logged in Terminal = tty7
Log in date = 2018-03-28
Log in time = 20:11

Shell script to check whether the given input is a file or directory

#Shell script to check whether the given input is a file or directory
for i in $*
do
    if  test -d $i
    then         
        echo "$i is a directory file"
    elif test -f $i
    then
        echo "$i is an ordinary file"
    else
       echo "$i does not exist"
    fi
done



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh fcheck.sh sxc well sasha
sxc does not exist
well is an ordinary file
sasha is a directory file

Shell script to display filenames in uppercase

#shell script to display filename to uppercase
if  [ $# -eq 0 ]
then
       echo “Pass an argument”
else
    for file in $*
    do
           if test -f $file
           then
                 echo  $file|tr ‘[a-z]’ ‘[A-Z]’
           else
                 echo “File $file does not exist”
           fi
    done
fi










OUTPUT
csc@csc-SVE1513CYNB ~ $ sh upper.sh well simple.c shan
WELL
SIMPLE.C
File shan does not exist

Shell script to add the numbers divisible by 3 in the range 50 to 100

#Shell script to add the numbers divisible by 3 in the range 50 to 100
s=0
for((i=50;i<=100;i++))
do
if [ `expr $i % 3` = 0 ]
then
     echo "$i "
     s=`expr $s + $i`
fi
done
echo  "Sum of the numbers divisible by 3 between 50 to 100 is $s"





OUTPUT
csc@csc-SVE1513CYNB ~ $ bash sumdiv.sh
51
54
57
60
63
66
69
72
75
78
81
84
87
90
93
96
99
Sum of the numbers divisible by 3 between 50 to 100 is 1275
 

Listing files having read, write and access permissions

#shell script to display files having rwx permission

echo "Files with Read, Write and Execute Permissions"
for file in *
do
if [ -f $file ]
then
   if [ -r $file -a -w $file -a -x $file ]
   then
       echo $file
   fi
fi
done



OUTPUT

csc@csc-SVE1513CYNB ~ $ sh access.sh
Files with Read, Write and Execute Permissions
a.out

Shell scrit to check whether the two file contents are same


#Shell script to compare the contents of two files
echo "Enter file 1"
read file1
echo "Enter file 1"
read file2
val=`cmp $file1 $file2`
if [ -z "$val" ]
then
   echo "Files have same content"
   rm $file1
   echo "$file1 removed"
else
   echo "File contents are different"
fi



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh same.sh
Enter file 1
file
Enter file 1
file
Files have same content
file removed

csc@csc-SVE1513CYNB ~ $ sh same.sh
Enter file 1
file2
Enter file 1
file3
File contents are different

Wednesday, 28 March 2018

Shell script to search for a number in a list of positional parameters

#shell script to check whether the given number is present
n=$#
i=0
echo "Enter the number to check: \c"
read num
while [ $n -gt 0 ]
do
     i=`expr $i + 1`
     if [ $1 -eq $num ]
     then
           pos=$i
           break;
    fi
    shift
    n=`expr $n - 1`
done
if [ $n -gt 0 ]
then
     echo "Number $num is present in the list at position $pos"
else
     echo "Number not in the list"
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh present.sh 1 2 3 4 5
Enter the number to check: 5
Number 5 is present in the list at position 5

csc@csc-SVE1513CYNB ~ $ sh present.sh 1 2 3 4 5
Enter the number to check: 6
Number not in the list

Shell script to display the last modification time of a file

#shell script to display the  last modification time of a file
echo  "Enter name of the file:"
read n
if  [ -e $n ]
then
        echo 'Last modification time is' `ls -l $n | cut -d" " -f 6,7,8`
else
        echo "file does not exist"
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh mtime.sh
Enter name of the file:
oddeven02.sh
Last modification time is Mar 28 15:21

Shell script to find the sum of odd and even numbers in an array separately


#shell script to sum the odd and even numbers in an array
echo "Enter the array elements "
read -a array
o=0
e=0
for((i=0;i<${#array[@]};i++))
do
    n=`expr ${array[$i]} % 2 `
    if [ $n -eq 0 ];then
          e=`expr $e + ${array[$i]}`
    else
          o=`expr $o + ${array[$i]}`
    fi
done
echo "The sum of even numbers in the given array.. $e"
echo "The sum of odd numbers in the given array..   $o"


OUTPUT
csc@csc-SVE1513CYNB ~ $ bash oddeven02.sh
Enter the array elements
1 2 3 4 5 6 7 8 9
The sum of even numbers in the given array.. 20
The sum of odd numbers in the given array.. 25

Shell script to count the number of occurrences of a given digit in a number



#Shell script to count the number of occurrences of a given digit in a number
count=0
echo "enter the number"
read n
num=$n
echo "Enter the number to be checked"
read f
while [ $n -gt 0 ]
do
    m=`expr $n % 10 `
    if [ $m -eq $f ];then
           count=`expr $count + 1`
    fi
    n=`expr $n / 10 `
done
echo "The number $f has occurred $count times in the number $num"


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh occur.sh
enter the number
12131425 
Enter the number to be checked
1
The number 1 has occurred 3 times in the number 12131425

Friday, 16 March 2018

Extract substring from a given string until the specified character

This program reads a string and a character and displays part of the string up-to the specified character.
Example
Given string
welcome
Given character
c
OUTPUT
wel

//Program to extract a sub string up-to a specified character

#include<stdio.h>
#include<stdlib.h>

int main()
{
      int i=0;
      char s[100],c;

      printf("enter string\n");
      scanf("%s",s);
      printf("enter character ");
      scanf(" %c",&c);
     while (s[i]!='\0')
    {
           if (s[i]==c)
                   break;
   
           printf("%c",s[i]);
              i++;
      }

    return 0;
}






OUTPUT
Enter string
good_morning
Enter character_
good

Note: After reading a string, the following scanf() to read a character will not wait for input, since the newline(\n) character at the end of the string is taken as an input to that character.
Hence,write the scanf() function to read the character as scanf(" %c",&c); 
Note there is blank space before % symbol. 

Wednesday, 14 March 2018

Counting common characters in two strings

This C program reads two strings s1 and s2 and outputs the number of characters that are common in both strings.
For example, if the input strings are
hello
welcome

output will be
3

Explanation
The common characters are e, l, o
Also a boundary condition is included which requires the length of the two strings must be between 3 and 100.

PROGRAM

#include<stdio.h>
#include <stdlib.h>

int main()
{
  char s1[100],s2[100],d[100];
  int i=0,j=0,n=0,m=0,count=0,k;

  printf("Enter two strings\n");
  scanf("%s%s",s1,s2);
  while(s1[i++] != '\0')
    n=i;
  while(s2[j++] != '\0')
    m=j;
    j=0;
  if ((n >= 3 && n<100) && (m >=3 && m < 100))
  {
          for(i=0;i<n;i++)

         {
              for(k=0;k<=j;k++)

             {
                  if(s1[i] == d[k])
                      break;
              }
              if(k>j)
                      d[j++]=s1[i];
         }
          d[j]='\0';

    for(i=0;i<=j-1;i++)
    {
          for(k=0;k<m;k++)
          {
             if(d[i] == s2[k])
             {
                 count++;

                 break;
             }
        }
    }   
 
  printf("\n%d",count);
  }

return 0;    
}

OUTPUT
Enter two strings
india
china
3

Monday, 12 March 2018

C program to rearrange the unique characters in a string in descending order

The following program reads a string as an input, displays the unique letters in the string in descending order.
For example, if the input is
innovation
the output of the program will be
vtonia

//Program to sort unique characters in a string

# include <stdio.h>

int main()
{
    char s[100],d[100],temp;
    int i=0,n,j=0,k;
    printf("Enter string\n");
    scanf("%s",s);
    while (s[i++]!='\0');
    n=i;
    if (n>=3 && n<100)
   {
       for(i=0;i<n;i++)
      {
          for(k=0;k<=j;k++)
         {
             if (s[i]==d[k])
             break;
         }
        if (k>j)
           d[j++]=s[i];
      }
       d[j]='\0';
  

      for(i=0;i<j-1;i++)
      for(k=i+1;k<j;k++)
       {
            if (d[i]<d[k])
            {
                temp=d[i];
                d[i]=d[k];
                d[k]=temp;
            }
       }
       printf("String after sorting\n");
       printf("-----------------------\n");
       printf("%s",d);
  }  
    return 0;
}


OUTPUT
Enter string                                                                                                                         
innovation   
                                                                                                                        
String after sorting                                                                                                                 
-----------------------                                                                                                              
vtonia