C Sample File : String

Strings :


// Program To Check Whether The String Is Pelindrom      Or Not Without Using String Functions

#include<stdio.h>
#include<conio.h>
void main()
{
     char str[50],rev[50];
     int i,j,c=0,len=0;
     clrscr();
     printf("Enter Any String : ");
     gets(str);
     for(i=0;str[i]!=NULL;i++)
     {
          len++;
     }
     j=0;
     for(i=len;i>=0;i--)
     {
          rev[j]=str[i];
          j++;
     }
     for(i=0;str[i]!=NULL;i++)
     {
          if(str[i]==rev[i])
          {
              c++;
          }
     }
     if(c==len)
     {
          printf("\nString Is Pelindrom !!!\n");
     }
     else
     {
          printf("\nString Is Not Pelindrom !!!\n");
     }
     getch();
}

Output :

Enter Any String : madam

String Is Pelindrom !!!

// Program To Check Whether The String Is Pelindrom Or Not With Using String Functions

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     char str[50],*rev;
     clrscr();
     printf("Enter Any String : ");
     gets(str);
     rev=strdup(str);
     if(strcmp(str,strrev(rev))==0)
     {
          printf("String Is Pelindrom !!!");
     }
     else
     {
          printf("String Is Not Pelindrom !!!");
     }
     getch();
}

Output :

Enter Any String : arora

String Is Pelindrom !!!


// Program To Compare Two Strings

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     char str1[20],str2[20];
     clrscr();
     printf("\nEnter Your First String          : ");
     gets(str1);
     printf("\nEnter Your Second String         : ");
     gets(str2);
     printf("\nAfter Comparison Result Is As    : ");
     if(strcmp(str1,str2)==0)
     {
          printf("Both Strings Are Matched !!!");
     }
     else
     {
          printf("Strings Are Not Matched !!!");
     }
     getch();
}

Output :

Enter Your First String          : Dharminder

Enter Your Second String         : Dharminder

After Comparison Result Is As    : Both Strings Are Matched !!!



// Program To Cut A String From A Given String

#include<stdio.h>
#include<conio.h>
void main()
{
     char str[50],cut[50],t;
     int i=0,j=0;
     clrscr();
     printf("Enter Your String            : ");
     gets(str);
     printf("Enetr String You Want To Cut : ");
     gets(cut);
     while(cut[j]!=NULL)
     {
          if(cut[j]==str[i])
          {
              t=str[i];
              str[i]=' ';
              i++;
              j++;
          }
          else
          {
              if(t!=' ')
              {
                   str[i-1]=t;
                   t=' ';
              }
              j=0;
              if(cut[j]==str[i])
              {
                   t=str[i];
                   str[i]=' ';
                   i++;
                   j++;
              }
              else
              {
                   i++;
              }
          }
     }
    

printf("String After Cut Is As       : ");
     for(i=0;str[i]!=NULL;i++)
     {
          if(str[i]==' ')
          {
          }
          else
          {
              printf("%c",str[i]);
          }
     }
     getch();
}

Output :

Enter Your String            : concatenate

Enetr String You Want To Cut : cat

String After Cut Is As       : conenate

Comments

Popular Posts