Sunday, February 25, 2018

C program : That replaces the occurence of a given character (say c) in a primary string (say PS) with another string (say s).

Input:
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)

Output:
Print the string PS with every occurence of c replaced by s.
 
 
 #include<stdio.h>
#include<string.h>

int main()
{
    char PS[1000],s[1000],ch;

    scanf("%s %c %s", PS,&ch,s);

    for(int i=0;i<strlen(PS);i++)
        {
            if(PS[i]==ch)
                {
                    for(int i=0;i<strlen(s);i++)
                        printf("%c",s[i]);
                }
            else
                printf("%c",PS[i]);
        }


    return 0;
}

Thanks
Happy Computing!

C program : Given two arrays of integers output the smallest number in the first array not present in the second one.

Input Specification: 
The first line contains the size N1 of the first array.
Next line give the contents of the first array.
Next line contains the size N2 of the second array.
Next line give the contents of the second array. 
 
Output Format:
Output must be a single number which is the smallest number occurring
in the first array that does not occur in the second. In case there is
no such number, output NO.
 
 #include<stdio.h>
# define MAX_SIZE 20

int main(){
int a[MAX_SIZE], b[MAX_SIZE],comn[MAX_SIZE];
int na,nb,count=0,g=0;
scanf("%d",&na);
if(na>0)
{
for(int i=0;i<na;i++)
{

    scanf("%d ",&a[i]);

}
scanf("%d",&nb);
for(int j=0;j<nb;j++)
{

    scanf("%d ",&b[j]);

}

for(int k=0;k<na;k++){

    for(int l=0;l<nb;l++)
    {
        if(a[k]==b[l])
        {
            count++;
        }


    }
    if(count==0)
    {
        comn[g]=a[k];
        g+=1;
    }
    if(count>=1)
    {
        ;
    }
    count=0;
}
int smallest=comn[0];
for(int j=0;j<g;j++)
{
    if (comn[j]<smallest)
    {
        smallest = comn[j];

    }

}
if(g!=0)
{
    printf("%d", smallest);
}
else
{
    printf("NO");
}
}
else
{
    printf("NO");
}

return 0;
}
 
Thanks
Happy Computing ! 
 

C program : To count the number of words in the sentence.

A line of English text will be given, where words are separated by one
of the following symbols:

 ' ', '\t', '.', ',' and ';'
 
You have to count the number of words
in the sentence.
 
 
 #include<stdio.h>

int main(){
  int  foundLetter = 0,count= -1,c;
 
    c = getchar();
 
    while ( c != EOF )
    {
       c = getchar();
     
      if(c == ' '||c =='.'||c =='\t'||c ==';'||c ==',')
      {foundLetter=0;}
      else
      {
          if (foundLetter == 0)
          count++;
          foundLetter = 1;
      }
    
      
    }
    printf("%d",count);
}
 
Thanks
Happy Computing !

C program: To list all the factorial numbers less than or equal to an input number n.

Input

A positive integer, say n.

Output

All factorial numbers less than or equal to n.
 
 #include<stdio.h>
int fac(int k)
{ if(k>1)
  {
      return k*fac(k-1);
  }
  if(k==1){return 1;}
}
int main(){
    int f=0,n;
  scanf("%d",&n);

    for(int j=1; j<=n;j++)
    {
        f=fac(j);
        if(f>n){break;}
         printf("%d ", f);
       
    }


}

Thanks
Happy Computing !

C program : A function to find the kth occurrence of an integer n in a sequence of non-negative integers

You are given the input in two lines:

The first line contains a non-negative integer, say n, and a positive
integer k, in that order. You have to find the kth occurrence of n in
the sequence below. (When

The second line consists of a sequence of non-negative integers,
terminated with a -1.  The -1 is not part of the sequence.


#include<stdio.h>


int find(int n, int k)
{
     int num, count= -1, ocurr=0;
  while(num != -1)
{
    scanf("%d", &num);
    count++;
    if(num==n)
    {
        ocurr++;

    }
    if(ocurr==k)
    {
        return count;
    }

  }
if(ocurr<k){return -1;}

}

int main(){
 int n,k,i;
  scanf("%d %d" , &n,&k);

  i=find(n,k);
   printf("%d",i);

}
Thanks
Happy Computing !

C program : To output the sequence of differences of adjacent pairs of numbers

You are given a sequence of numbers, ending with a -1. You can assume
that are at least two numbers before the ending -1. 

Let us call the sequence x0 x1 ... xn -1.

You have to output the sequence of differences of adjacent pairs of
numbers, as follows: 

x1-x0 x2-x1 ... x_{n}-x_{n-1}


#include<stdio.h>

int main()
{
      int a ,b,diff;
  scanf("%d", &a);
  if(a !=(-1))
  {   
    while(b!= (-1))
  {
       scanf("%d", &b);
      if(b== -1){break;}
    diff=(b-a);
    printf("%d ",diff);
           a=b;
  }
  }
  return 0;
}

Thanks
Happy Computing !

C program: Find the second largest number in the input. Without using arrays.

You are given a sequence of integers as input, terminated by a
-1. (That is, the input integers may be positive, negative or 0. A -1
in the input signals the end of the input.) 

-1 is not considered as part of the input.  

#include<stdio.h>

int main(){
int largest=0,firstInput=0,secondInput=0,secondLargest=0;
scanf("%d",&firstInput);
if(firstInput != (-1))
 {
   
    scanf("%d",&secondInput);
    while(secondInput !=(-1))
    { 
        if(secondInput>firstInput)
        {
        largest=secondInput;
        secondLargest=firstInput;
        firstInput=largest;
        }
        else
        {  
          if(secondInput>secondLargest)
          secondLargest=secondInput;
        }

        scanf("%d", &secondInput);
    }


}
printf("%d",secondLargest);
  return 0;
}

Thanks
Happy computing !

what is crystal field theory and why do we need it?

  Calculating CFSE