string programs in c


1) C Program to Find the Frequency of Character in a String

#include<stdio.h>
int main()
{
    char a[100],ch;
    int i,cnt=0;
    printf("enter string: ");
    gets(a);
    printf("enter char: ");
    scanf("%c",&ch);
  
    for(i=0;a[i];i++)
    {
       if(a[i]==ch)
       {
           cnt++;
       }
    }
   printf("the frequency of the %c is %d ",ch,cnt);
   return 0;

}

 //////////////////////////////////////////////////////////////////////////////////

 2) C Program to Reverse a sentence using recursion

 #include <stdio.h>
 void reverse()
 {
    char ch;
   
    scanf("%c", &ch);
    if( ch!= '\n' )
     {
        reverse();
        printf("%c",ch);
     }
 }

 int main()
 {
    char a[100];
    printf("enter string:");
    reverse();
    return 0;
 } 

////////////////////////////////////////////////////////////////////////////////


3)C Program to Remove Characters in String Except Alphabets


#include <stdio.h>

int main()
{
  char a[100];
  int i;
  printf("enter string:");
  gets(a);
  for(i=0;a[i];i++)
  {
      if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122))
      continue;
      else
      {
          a[i]=0;
          i--;
      }
      printf("result:%s",a);
  }

//////////////////////////////////////////////////////////////////////////////////////////


4) C Program to Sort Strings in Dictionary Order


#include <stdio.h>

int main()

{

     int i, j;

    char a[10][50], temp[50];

    printf("Enter 10 words:\n");

    for(i=0; i<10; ++i)

     {

      scanf("%s",a[i]);
     }
    for(i=0; i<9; ++i)
    {
       for(j=i+1; j<10 ; ++j)
        {
            if(strcmp(a[i], a[j])>0)
            {
                strcpy(temp, a[i]);
                strcpy(a[i], a[j]);
                strcpy(a[j], temp);
          }
        }
    }
    printf("\nIn lexicographical order: \n");
    for(i=0; i<10; ++i)
     {
        printf("%s\t",a[i]);
     }
    return 0;
}
  

/////////////////////////////////////////////////////////////////////////////// 

5) C Program to remove Duplicate Element in an Array

 

#include <stdio.h>

int main()

{

    int  a[200],i,j,k,n;

    printf("enter n value:");

    scanf("%d",&n);

    printf("enter the %d elements:",n);

    for(i=0;i<n;i++)

     {

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

    }

   

    for(i=0;i<n;i++)

     {

         for(j=i+1;j<n;j++)

           {

            if(a[i]==a[j])

            {

              for(k = j; k < n; k++)

               {

                a[k] = a[k+1];

               }

                n--;

             }

           }

    }

    for(i=0;i<n;i++)

     {

       printf("%d",a[i]);

     }

    return 0;

}

//////////////////////////////////////////////////////////////////////
 





Comments

Popular Posts