New Android apps UniqueKey

Sunday, 22 February 2015

Removing white space from a string.


#include<stdio.h>

main()
{
    char str[150];
    int i,j;
    
    printf("Enter a string: ");
    gets(str);
    
    for(i=0; str[i]!='\0'; i++)
    {
        while (!((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z' || str[i]=='\0')))
        {
            for(j=i;str[j]!='\0';++j)
            {
                str[j]=str[j+1];
            }
            str[j]='\0';
        }
    }
    printf("Output String: ");
    puts(str);
}

Find no. of vowels , consonants , digits and white spaces in a string.


#include<stdio.h>

main()
{
    char str[150];
    int i,v,c,ch,d,s,o;
    
    o=v=c=ch=d=s=0;
    
    printf("Enter a string:\n");
    gets(str);
    
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
            ++v;
        else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
            ++c;
        else if(str[i]>='0'&&c<='9')
            ++d;
        else if (str[i]==' ')
            ++s;
    }
    printf("Vowels: %d",v);
    printf("\nConsonants: %d",c);
    printf("\nDigits: %d",d);
    printf("\nWhite spaces: %d",s);
}

No. of particular character in a string.


#include <stdio.h>

main()
{
   char c[100],ch;
   int i,count=0;
   
   printf("Enter a string: ");
   gets(c);
   printf("Enter a character to find frequency: ");
   scanf("%c",&ch);
   
   for(i=0;c[i]!='\0';i++)
   {
       if(ch==c[i])
           count++;
   }
   printf("Frequency of %c = %d", ch, count);
}

Prime numbers b/w given interval


/* A C program to display all prime numbers between given interval. */

#include <stdio.h>

main()
{
  int n1, n2, i, j, c;
  
  printf("Enter two numbers :   ");
  
  scanf("%d %d", &n1, &n2);
  printf("Prime numbers between %d and %d are: ", n1, n2);
  for(i=n1+1;i<n2;i++)
  {
      c=0;
      for(j=2;j<=i/2;j++)
      {
        if(i%j==0)
        {
          c=1;
          break;
        }
      }
      if(c==0)
        printf("%d ",i);
  }
}

Prime Number or not


/* A C program to check whether a number is prime or not. */

#include<stdio.h>

main()
{
  int n, i, c=0;
  
  printf("Enter the number to check whether this number is prime or not :  ");
  scanf("%d",&n);
  
  for(i=2;i<=n/2;i++)
  {
      if(n%i==0)
      {
          c=1;
          break;
      }
  }
  if (c==0)
      printf("%d is a prime number.",n);
  else
      printf("%d is not a prime number.",n);
}