January 30, 2013

Strings in C++ (OOPs)


various strings operation that are stored under header string.h

The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. Character arrays are many a time also called strings. Many languages internally treat strings as character arrays, but somehow conceal this fact from the programmer. Character arrays or strings are used by programming languages to manipulate text such as words and sentences.


A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). The terminating null (‘\0’) is important, because it is the only way the functions that work with a string can know where the string ends. In fact, a string not terminated by a ‘\0’ is not really a string, but merely a collection of characters.


Example of sample formats mainly employed for developing programs:


If you wanna enter any single city name:

char ch[data size];

cout<<”\n Enter city name”;

cin>>ch; [or use gets(ch);]


if you wanna enter multiple city names:

char ch[data size][number of cities];

cout<<”\n Enter city names”;

for(i=1;i<=number of city names;i++)

{

cin>>ch[i];

}


Types of Operations can be performed with strings

1. Reading and writing of strings either its data or size.

2. Combining two strings like 1st string “My Name is” and 2nd string “Aman Bansal”, so combination gives rise to “My Name is Aman Bansal”.

3. Copy one string to another as similarly you do with files from one PC to another.

4. Comparing two strings on basis of size, upper or lower case alphabets and many other.

5. Extracting a sub-string from a complete string.



strlen()

This function calculates the length of the string it means it can count the total number of character presents in the string which includes alphabets, numbers and all special characters.


strcat()

This function can join two strings. It actually joins the second string at the end of the first string. Hence, by this function act as a junction for two strings.


strcpy()

This function will copy one string over the another string. It means second string will overwrite over the first string and value content of first string will be changed by the second string.


strcmp()

This function compares two strings and if two strings are equal then it gives result as 0 but if two strings differ then it gives a non-negative number.


strcmpi()

This function compares two string after ignoring the case of an alphabet it means it consider upper case alphabets and lower case alphabets as same and if both the string are same then it gives result as zero otherwise a non-negative number.


/* Program to enter any string and find its length*/


#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
      char st[100][100];
      int a[2],i;
      for(i=1;i<=2;i++)
      {
                       cout<<"\n Enter any string\n ";
                       gets(st[i]);
                       }
      for(i=1;i<=2;i++)
      {
             a[i]=strlen(st[i]);
             cout<<"\n String is: "<<st[i]<<" having length of "<<a[i];
                       }
      getch();
      }
output of program to enter any string and find its length using string commands in C++ (OOPs)

/* Program to enter any string and find its length without spaces*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
      char st[50];
      int i,ct=0,n;
      cout<<"\n Enter any string \n ";
      gets(st);
      n=strlen(st);
      for(i=0;i<n;i++)
      {
                      if(st[i]==' ')
                      ct++;
                      }
      if(ct==0)
      cout<<"\n The string has "<<n<<" length";
      else
      {
          int a=n-ct;
          cout<<"\n The string has "<<a<<" length";
          }
      getch();
      }
output of program to enter any string and find its total characters using strings in C++ (OOPs)

/* Program to enter any string and find number of vowels in it*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
      char st[100],vow[5]={'a','e','i','o','u'};
      int i,j,n,ct=0;
      cout<<"\n Enter any string (Must be in lower case)\n ";
      gets(st);
      n=strlen(st);
      for(j=0;j<5;j++)
      {
                      for(i=0;i<n;i++)
                      {
                                      if(vow[j]==st[i])
                                      ct++;
                                      }
                                      }
      if(ct==0)
      cout<<"\n There are "<<ct<<" vowels in the string "<<"\""<<st<<"\"";
      else
      cout<<"\n There are "<<ct<<" vowels in the string "<<"\""<<st<<"\"";
      getch();
      }
output of program to enter any string and find number of vowels in the string in C++ (OOPs)
      

/* Program to enter any string and prints its reverse string */

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
      char st1[50],st2[50];
      int a,i;
      cout<<"\n Enter any string\n ";
      gets(st1);
      a=strlen(st1);
      cout<<"\n Reverse string is: ";
      for(i=a;i>=0;i--)
      {
                       cout<<st1[i];
                       }
      getch();
      }
output of program to enter any string and print it in reverse order in C++ (OOPs)
                 

/* Program to enter any string and check its palindrome or not */

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
      char st1[50];
      int a,i,j,ct=0;
      cout<<"\n Enter any string to check whether its palindrome or not\n ";
      gets(st1);
      a=strlen(st1);
      cout<<"\n Original String: "<<st1;
      cout<<"\n Reverse String: ";
      for(i=a;i>=0;i--)
      {
                       cout<<st1[i];
                       }
      i=a;
      for(j=1;j<=a;j++)
      {
                       if(st1[j]==st1[i])
                       ct++;
                       i--;
                       }
      if(ct==0)
      cout<<"\n String is not palindrome";
      else
      cout<<"\n String is palindrome";
      getch();
      }
output of program to enter any string and check whether its palindrome or not in C++ (OOPs)

No comments:

Post a Comment