Saturday 6 April 2019

Shell script to read in two numbers and display the number divisible by the first number in the given range

Program
echo "Enter two numbers"
read m n
echo "Numbers divisible by [$m] in the range [$m] to [$n]"
for((i=$m;i<=$n;i++))
do
    div=`expr $i % $m`
    if test $div -eq 0
    then
         echo $i
    fi
done



OUTPUT
Safi $ bash samp.sh
Enter two numbers
3 20
Numbers divisible by [3] in the range [3] to [20]
3
6
9
12
15
18

Count number of files and sub-directories in a given directory

Shell script to input a directory name and display the number of files and
sub directories present in the given directory.


Program
echo -n "Enter directory name: "
read dr
dircnt=`ls -l $dr|grep "^d"|wc -l`
filecnt=`ls -l $dr|grep "^-"|wc -l`
echo "No of directories = $dircnt"
echo "No of files = $filecnt"


OUTPUT
Safi $ sh dircnt.sh
Enter directory name: .
No of directories = 10
No of files = 98


Safi $ sh dircnt.sh
Enter directory name: sasha
No of directories = 0
No of files = 4

Friday 5 April 2019

Shell script that asks for a word and a file name and then tells how many lines contain that word.

echo "Enter word,filename";
 read word filename
n=`grep -c "$word" $filename`;
echo "The word [$word] in the $filename has occurred in $n lines";




OUTPUT
Safi>sh eb.sh
Enter word,filename
word eb.sh
The word word in the eb.sh has occured in 4 lines

Safi>sh eb.sh
Enter word,filename
for eb.sh
The word for in the eb.sh has occured in 0 lines

Thursday 4 April 2019

Shell script to read a username and display how many times a user has logged in.

#Counting the number of times a user has logged in
echo "Enter the username"
read uname
times=`who | grep  "$uname" | wc -l`
if [ $times -ne 0 ]
then
   echo "$uname has logged in $times times"
else
   echo "user [$uname] not logged in"
fi

 
OUTPUT
Safi>sh eb.sh
Enter the username
csc
csc has logged in 3 times
Safi>sh eb.sh
Enter the username
sss
user [sss] not logged in

Shell script to perform linear search in an array

#Check whether a number is present or not
echo -n "Enter limit n: "
read n
for((i=0;i<n;i++))
do
 read a[$i]
done
echo ${a[*]}
echo -n "Enter number to be searched: "
read key
pos=0
for((i=0;i<n;i++))
do
  if test $key -eq ${a[$i]}
  then
      pos=`expr $i + 1`
      break;
  fi
done
if test $i -ge $n
then
  echo "Number not found"
else
  echo "Number [$key] found at position $pos"
fi





OUTPUT
Safi>bash numsrch.sh
Enter limit n: 5
7
6
8
5
9
7 6 8 5 9
Enter number to be searched: 7
Number [7] found at position 1

Shell script to count the vowels in the given string

#script to count the number of vowels in the given string using if
echo Enter string
read str
count=0
len=`echo $str | wc -c`
len=`expr $len - 1`
while test $len -gt 0
do
     temp=`echo $str | cut -c $len | tr [A-Z]  [a-z]`
     len=`expr $len - 1`
     if test  "$temp" = "a" -o "$temp" = "e" -o "$temp" = "i" -o
            "$temp" ="o"  -o  "$temp" = "u"
     then
                           echo “$temp”
                           count=`expr $count + 1`
     fi
done
echo Number of vowels in the given string \"$str\" are "$count"
 


OUTPUT
Safi> sh vwlcnt.sh
Enter string
good
“o”
“o”
Number of vowels in the given string "good" are 2

 

Sunday 17 March 2019

Sum between 5 and 10

The program must accept N integers as the input. The program must traverse the given integers and if the integer 5 is encountered then the following integers must be added till the integer 10 occurs. Finally, the program must print the total sum as the output.

Boundary Condition(s):
1 <= N <= 1000

Input Format:
The first line contains N.
The second line contains N integers separated by space(s).

Output Format:
The first line contains the sum as per the given conditions.

Example Input/Output 1:
Input:
9
2 5 3 2 10 3 10 5 4
Output:
9
Explanation:
The integer 5 occurs at position 2 and the immediate next integer value 10 occurs at position 5. There are two integers (3 and 2) between them which are added to 5.
The next integer value 5 occurs at position 8 the integers after 5 is 4 which is also added to the result.
Hence the output is 9

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

int main()
{
   int N,A[1000],i,j=0,temp=0;
   scanf("%d",&N);
   for(i=0;i<N;i++)
      scanf("%d",&A[i]);
   for(i=0;j<N;)
   {
       if(A[i]==5)
       {
          while(A[++i]!=10)
          {
              temp+=A[i];
              if (i>=N)
              break;
          }      
        }
       j=++i;
   }      
   printf("%d",temp);

}

Output
Input:
8
1 5 5 12 10 5 6 10
Output:
23

Monday 25 February 2019

Count of common factors

Given a set of numbers where all other numbers are multiple of the smallest number, the program must find the count of the common factors C excluding 1.

Input Format:
First line will contain the integer value N representing how many numbers are passed as input.
Next N lines will have the numbers.

Output Format:
First line will contain the count of common factors C.

Constraints:
N will be from 2 to 20.

Sample Input/Output:
Example 1:
Input:
2
100
75

Output:
2
Explanation:
The common factors excluding 1 are 5,25. Hence output is 2




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

int main()
{
   int N,A[100],i,cnt=0,j,small;
   scanf("%d",&N);
   for(i=0;i<N;i++)
      scanf("%d",&A[i]);
    small=A[0];
    for(i=0;i<N;i++)
      if (A[i]<small)
        small=A[i];
   j=2;
   while (j<=small)
   {
     for(i=0;i<N;i++)
      if (A[i]%j!=0)
         break;
      if (i==N)
        cnt++;
     j++;
   }
   printf("%d",cnt);

}

OUTPUT
Input:
3
10
20
30
Output:
3

Thursday 21 February 2019

Print from X to Y - Increment by 0.1

The program must accept two float values X and Y as the input. The program must print the values from X to Y where the values must be incremented by 0.1.

Boundary Conditions:
0.0 <= X, Y <= 100.0

Input Format:
The first line contains X and Y separated by a space.

Output Format:
The first line contains the values separated by a space.

Example Input/Output 1:
Input:
3.2 3.8
Output:
3.2 3.3 3.4 3.5 3.6 3.7 3.8

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

int main()
{
   float X,Y;
   int cnt;
   scanf("%f%f",&X,&Y);
   cnt=(Y*10-X*10);
   while(cnt>=0)
   {
      printf("%.1f ",X);
      X=X+0.1;
      cnt--;
   }  
}

Output

9.4 10.2

9.4 9.5 9.6 9.7 9.8 9.9 10.0 10.1 10.2

Thursday 14 February 2019

Expand Alphabets

A string S is passed as input. S will contain multiple integer values with each integer value followed by an alphabet. The program must expand the alphabets based on the related integer value.

Input Format:
The first line contains S.

Output Format:
The first line contains the expanded string value.

Boundary Conditions:
Length of S is from 2 to 100.

Example Input/Output 1:
Input:
4a5h
Output:
aaaahhhhh
Explanation:
As it is 4a and 5h, four a's are printed followed by 5 h's

Example Input/Output 2:
Input:
1k2b4k
Output:
kbbkkkk

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

int main()
{
   char S[100],num[100];
   int i=0,j=0,cnt=0;

   scanf("%s",S);
   while(S[i]!='\0')
   {
       
       if (isdigit(S[i]))
       {
          j=0;
          while(S[i]>='0' && S[i]<='9')
            num[j++]=S[i++];
            num[j]='\0';
            cnt=atoi(num);
        }  
        
            while (cnt>0)
            {
               printf("%c",S[i]);
               cnt--;
            }  
            ++i;
   }  
}

OUTPUT
100q2w
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqww

Wednesday 13 February 2019

Palindrome Missing Alphabet

String S which is a palindrome is passed as the input. But just one alphabet A is missing in S. The program must print the missing alphabet A.
Note: The FIRST alphabet of S will always be present.

Input Format:
The first line contains S.

Output Format:
The first line contains the missing alphabet A.

Boundary Conditions:
The length of the input string S is between 3 to 100.
The FIRST alphabet of S will always be present.

Example Input/Output 1:
Input:
malayaam
Output:
l

Example Input/Output 2:
Input:
abcddcb
Output:
a

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

int main()
{
   char S[100];
   int i,j,a;
   scanf("%s",S);
   a=strlen(S);
   if (S[0]!=S[a-1])
   {
         printf("%c",S[0]);
         exit(0);
   }
   for(i=0,j=a-1;i<a/2;i++,j--)
   {
       if (S[i]!=S[j])  
       {
          if (S[i+1]==S[j])
                   printf("%c",S[i]);
           else
                  printf("%c",S[j]);
           break;
         }
  }     
}

OUTPUT
malayaam
l

Saturday 9 February 2019

Equal First and Second Half String

The program must accept a string S as the input. The program must print yes if the first half of the string is equal to the second half of the string. Else the program must print no as the output.
Note: If the length of the string is odd ignore the character in the middle.

Boundary Condition(s):
1 <= Length of the string <= 1000

Input Format:
The first line contains the string S.

Output Format:
The first line contains either yes or no.

Example Input/Output 1:
Input:
dumdum
Output:
yes
Explanation:
Both the first and second half of the string is same so yes is printed.

Example Input/Output 2:
Input:
YelYe
Output:
yes

Program
#include <stdio.h>

int main()
{
    char S[1000];
    int i,j=0,a;
    scanf("%s",S);
    a=strlen(S);
    if(a%2==0)
       j=a/2;
    else
       j=a/2+1;
    for(i=0;i<a/2;i++)
    {
        if(S[i]!=S[j++])
        {
            printf("no");
            break;
        }
    }    
    if(i==a/2)
        printf("yes");
            
}


Output:
tellet
no

Thursday 7 February 2019

Reverse String Till Underscore

String S is passed as the input to the program. S may or may not have a single underscore embedded in it. The program must reverse the String S till the first underscore and print it as the output.

Input Format:
The first line contains S.

Output Format:
The first line contains the string S modified based on the given conditions.

Boundary Conditions:
Length of S is from 3 to 100.

Example Input/Output 1:
Input:
abcd_pqrs
Output:
dcba_pqrs

Example Input/Output 2:
Input:
_kilo
Output:
_kilo

Example Input/Output 3:
Input:
nounderscore
Output:
erocsrednuon

Program

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

int main()

{
  char s[100],rev[100];
  int i=0,j=0,m,fnd=0;
  scanf("%[^\t\n]s",s);
  
  while(s[i] != '\0' && s[i] != '\n')
  {
     
     if (s[i]!='_')
       rev[j++]=s[i];
     else
        {
            for(j=j-1;j>=0;j--)
            printf("%c",rev[j]);
            fnd=1;
        }
        if (fnd==1)
            printf("%c",s[i]);
    ++i;
  }
  if (fnd==0)
  {
    for(j=strlen(s)-1;j>=0;j--)
    printf("%c",s[j]);
  }



OUTPUT
fsdf 35345 sfsdfsdf_good
fdsfdsfs 54353 fdsf_good

Wednesday 6 February 2019

Print Only Alphabets

A string S is passed as the input. S can contain alphabets, numbers and special characters. The program must print only the alphabets in S.

Input Format:
The first line contains S.

Output Format:
The first line contains only the alphabets in S.

Boundary Conditions:
The length of the input string is between 1 to 1000.

Example Input/Output 1:
Input:
abcd_5ef8!xyz
Output:
abcdefxyz

Program
#include <stdio.h>

int main()
{
    char S[1000];
    int i=0;
    
    scanf("%[^\t\n]s",S);
    
    while (S[i]!='\0' && S[i]!='\n')
    {
        if ((S[i]>='A' && S[i]<='Z') || (S[i]>='a' && S[i]<='z'))
        printf("%c",S[i]);
        ++i;
    }

    return 0;
}


Output:
Input:
jhjkhj564564   jk j|dg

jhjkhjjkjdg



Sunday 27 January 2019

Binary Representation - Squares

The program must accept an integer N as the input. The program must print the binary representation of the square of each integer from 1 to N as the output.

Boundary Condition(s):
1 <= N <= 1000

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the binary representation of squares from 1 to N are separated by space(s).

Example Input/Output 1:
Input:
4
Output:
1 100 1001 10000

Explanation:
The square of 1 is 1. So the binary representation of 1 is 1.
The square of 2 is 4. So the binary representation of 4 is 100.
The square of 3 is 9. So the binary representation of 9 is 1001.
The square of 4 is 16. So the binary representation of 16 is 10000.
Hence the output is 1 100 1001 10000

Program

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

int main()
{
   int N,a[1000],i,j,k=0;
   scanf("%d",&N);
   for(i=1;i<=N;i++)
   {
        j=i*i;
        k=0;
       while(j>0)
       {
           a[k++]=j%2;
           j=j/2;
       }
       for(k=k-1;k>=0;k--)
       printf("%d",a[k]);
       printf(" ");
   }

}

Output

9

1 100 1001 10000 11001 100100 110001 1000000 1010001

Monday 21 January 2019

Positive Negative Series

The program must accept an integer N as the input. The program must print the sum of the first N terms in the series given below.
The order of the series must be 1, -2, 3, -4, 5, -6 and so on (All the odd terms in the series formed from the positive odd numbers and all the even terms in the series formed from the negative even numbers).

Boundary Condition(s):
1 <= N <= 10^4

Input Format:
The first line contains the integer N.
Output Format:
The first line contains the sum of the first N terms in the above mentioned series.

Example Input/Output 1:
Input:
5
Output:
3
Explanation:
The first 5 terms are 1, -2, 3, -4 and 5 and their sum is 3 (1 - 2 + 3 - 4 + 5).
Hence the output is 3

Program

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

int main()
{
       
   int sum=0,i,j,N;
   scanf("%d",&N);
   for(i=1;i<=N;i+=1)
   {
       if (i%2==0)
            sum=sum + (-1)*i;
       else
            sum=sum+i;
   }
   
   printf("%d",sum);


}

Output:
Input:
8

Output:
-4

Sunday 20 January 2019

Sum of every X integers


The program must accept N integers and an integer X (where X is the factor of N) as the input. The program must print the sum of every X integers among N integers as the output.
Boundary Condition(s):
1 <= N <= 1000
1 <= Each integer value <= 1000
1 <= X <= N
Input Format:
The first line contains the integer N.
The second line contains N integers separated by space(s).
The third line contains the integer X.
Output Format:
The first line contains the sum of every X integers among N integers separated by a space.
Example Input/Output 1:
Input:
8
2 3 4 9 8 7 1 5
4
Output:
18 21
Explanation:
The first 4 integers are 2, 3, 4 and 9. So their sum is 18.
The next 4 integers are 8, 7, 1 and 5. So their sum is 21.
Hence the output is 18 21

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

int main()
{
   int N,A[1001],i,X,b=0,c=0,cnt=0;
   scanf("%d",&N);
   for(i=0;i<N;i++)
      scanf("%d",&A[i]);
   scanf("%d",&X);
   cnt=X;
   for(i=0;i<N;i++)
   {
       b+=A[i];
       --cnt;
       if (cnt==0)
       {
        printf("%d ",b);
        b=0;
        cnt=X;
       }
       
   }

}

Input/Output:
Input:
9
21 7 46 37 5 73 6 8 9
1
Output
21 7 46 37 5 73 6 8 9

Thursday 17 January 2019

Toggle Case - Vowels

The program must accept a string S as the input. The program must toggle the case of vowels in the string S. Then the program must print the modified string as the output.

Boundary Condition(s):
1 <= Length of S <= 100

Input Format:
The first line contains the string S.

Output Format:
The first line contains the modified string.

Example Input/Output 1:
Input:
EquilIbriUm

Output:
eqUIlibrIum

Explanation:
The vowels in the string "EquilIbriUm" are 'E', 'u', 'i', 'I', 'i' and 'U'.
So toggle the case of all the vowels in the string "EquilIbriUm".
Hence the output is eqUIlibrIum

Example Input/Output 2:
Input:
JUNKVIRUS

Output:
JuNKViRuS

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

int main()
{
  char S[100];
  int i=0;
  scanf("%s",S);
  
  while(S[i]!='\0')
  {
    switch (S[i])
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u': S[i]=toupper(S[i]);
                  break;
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U': S[i]=tolower(S[i]);
                  break; 
    } 
    ++i;
  }
  printf("%s",S);
     
      
}

OUTPUT
EvaluAte
evAlUatE

C program to display a string until a specified character

The program gets the string S and a character C as input. The characters in the input string are displayed until the specified input character in the string.

For example,
Input
Evaluate
l
Output
Eva

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

int main()
{
  char S[100],C;
  int i=0;
  scanf("%s %c",S,&C);  //Leave a space between %s and %c to skip enter key input
    
  while(S[i]!=C)
  {
      printf("%c",S[i]);
      ++i;
    }    
      
}

C Program to reverse middle K characters

The program must accept a string S and an integer K as the input. The program must reverse the middle K characters in S. Then the program must print the modified string as the output.
Note:  The length of S and the integer K are always either odd or even.

Boundary Condition(s):

1 <= Length of S <= 100
1 <= K <= Length of S

Input Format:

The first line contains the string S.
The second line contains the integer K.

Output Format:
The first line contains the modified string.

Example
Input:
acknowledgement
7

Output:
acknegdelwoment

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

int main()
{
   int K,cnt=0,i=0,j,a,l;
   char S[100],tmp[100];
   scanf("%s%d",S,&K);
   a=strlen(S);
   if ((a%2==0 && K%2==0) || (a%2!=0 && K%2!=0))
   {
        cnt=K;
        j=(a-K)/2;
        l=j;   
        while (cnt>0)
       {
                     tmp[i++]=S[l++];
                      cnt--;
       }
       tmp[i]='\0';
      for(l=j,i=i-1;i>=0;--i)
                 S[l++]=tmp[i];
       printf("%s",S);
   }
}

OUTPUT
example
3

expmale