Followers

Showing posts with label STRING. Show all posts
Showing posts with label STRING. Show all posts

Monday, March 12, 2012

COMPARING 2 STRINGS FOR EQUALITY

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>


int len1, len2;

void equal(char a[50], char b[50])
{
  if(len1==len2)
  {
    for(int i=0 ; i<len1 ; i++)
    {
      if(a[i]==b[i])
      continue;
      else
      {
      cout<<"NOT EQUAL";
      getch();
      exit(0);
      }
    }
    cout<<"EQUAL";
  }
  else
    cout<<"NOT EQUAL";

}

void main()
{
  char strng1[25], strng2[25];
  cout<<"enter a string : ";
  gets(strng1);
  len1=strlen(strng1);
  cout<<"enter another string : ";

  gets(strng2);
  len2=strlen(strng2);
  equal(strng1, strng2);

  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

REVERSING A STRING-WITHOUT USING ANOTHER STRING

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void main()
{
  char strng[25];
  int len;
  cout<<"enter a string : ";
  gets(strng);
  len=strlen(strng);
  cout<<"\n\nreverse : \n";
  for(int i=len-1; i>=0 ; i--)
    cout<<strng[i];
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

SEARCH LETTER IN A PARAGRAPH

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>

void main()
{

  char strng1[100], x;
  int len1, len2;
  cout<<"enter a string : ";
  gets(strng1);
  len1=strlen(strng1);
  cout<<"enter the letter to be searched : ";
  cin>>x;
  {
    textcolor(2);
    textbackground(0);
  }
  for(int i=0 ; i<len1 ; i++)
    {
      if(strng1[i]==x)
      {
      cprintf("(");
      cout<<strng1[i];
      cprintf(")");
      }
      else
      cout<<strng1[i];
    }

  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

Friday, January 6, 2012

REVERSING A STRING-USING ANOTHER STRING


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void main()
{
  char strng[25], rev[25];
  int len;
  cout<<"enter a string : ";
  gets(strng);
  len=strlen(strng);
  for(int i=0 ; i<len ; i++)
  {
    rev[i]=strng[len-1-i];
  }
  cout<<"\n\n reverse :\n";
  puts(rev);

  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

Thursday, January 5, 2012

CASE CONVERSION IN A STRING - USING LOGIC

#include<iostream.h>
#include<conio.h>
#include<string.h>

char strng[50];
int len, n;

void cmpltcnvrsion(char strng[50])
{
  for(int i=0 ; i<len ; i++)
  {
    if((97<=int(strng[i]))&&(int(strng[i])<=122))
      cout<<char(int(strng[i])-32);

    else if((65<=int(strng[i]))&&(int(strng[i])<=90))
      cout<<char(int(strng[i])+32);

    else
      cout<<strng[i];
  }
}

void capital(char strng[50])
{
  for(int i=0 ; i<len ; i++)
  {
    if((97<=int(strng[i]))&&(int(strng[i])<=122))
      cout<<char(int(strng[i])-32);
    else
      cout<<strng[i];
  }
}

void small(char strng[50])
{
  for(int i=0 ; i<len ; i++)
  {
    if((65<=int(strng[i]))&&(int(strng[i])<=90))
      cout<<char(int(strng[i])+32);

    else
      cout<<strng[i];
  }

}

void main()
{
  clrscr();
  cout<<"Enter a string :\n";
  cin.getline(strng,50);
  len=strlen(strng);
  cout<<"\n\nChoose the type of conversion : \n 1>Complete conversion \n 2>ALL CAPITAL \n 3>all small \nEnter your choice : ";
  cin>>n;
  switch(n)
  {
    case 1  : cmpltcnvrsion(strng);
    break;
    case 2  : capital(strng);
    break;
    case 3  : small(strng);
    break;
  }
  getch();
}
.................................................................
OUTPUT: (click to enlarge the image)