String Manipulation in C++ || Strings || C++


String Manipulation means handling or Manipulating the process and the operation of the strings. For example :- Joining two strings, Copying strings, Converting a string to Lower Case and many more.

Now before starting and learning about the functions and manipulation of strings first let's get some information about the strings in C++ Programming Language.


What are strings ?

Strings in Programming are simply the characters which are basically not integers numbers which means all the variables are strings until they are defined by any data type like 'int' or 'float' etc which make them numerical type or number type. To declare a string we use data types like 'char'. In fact when we talk about the arrays too the elements of the array are fundamentally of char type i.e. strings unless they are declared as any other type. 

In C++ Programming string is the sequence of characters with a null character (\0), it usually appends the null character by default rather than asking user to enter it everytime while entering the characters. The reason behind the null character is that in Programming languages '0' is considered as 'False' in Boolean while '1' is considered as 'True' so it become easy for the C++ language to manipulation of the string because it will do the operation on the given or entered string till the value turns out to be 'False' or '0'.

Example :-  char c[] = "helpforcoders";

In the above example suppose we are doing any string manipulation then the compiler will check for the characters till it reach the last one and then it will terminate the operation because by default '\0' is added to the above string. The above string can also be written as : char c[] = "helpforcoders\0" then also it won't give any error...


All types of string Manipulation : -

Strcpy :- It is called as 'String copy ' function. It is used to copy a string from one variable to another variable.

Syntax :- strcpy(string 1, string 2); 

Strcat :- It is called as 'String concatenate ' function. It is used to concatenate two strings.

Syntax :- strcat(string 1, string 2);

Strcmp :- It is called as 'String compare ' function. It is used to compare two strings.

Syntax :- strcmp(string 1, string 2); 

Strlen :- It is called as 'String length ' function. It is used to count the length of the string.

Syntax :- strlen(string 1);


Example or Program of string Manipulation :-


     
 // special header file for string manipulation
#include<iostream>
#include <string.h>
using namespace std;
int main()
{
   // daclaring variables
   char destination[25];
   char *a = "Helpfor", *b = "coders";
   int x,y;

   // syntax for copying string or string copying function
   strcpy(destination, a);
   cout<<"The copied string is "<<destination;

   // string for concatinating\joining two strings or string concatination function
   strcat(a, b);
   cout<<"\nThe concatinated or combined string is" << a;

   // using x variable to compare two strings with string compare function
   x = strcmp(a,b);

   // conditional statement to print the bigger string
   if(x<0)
   {
   cout<<"\n"<<a<<" is bigger \n";
   }
   else
   cout<<"\n"<<b<<" is bigger\n";

   // copying variable a to destination and finding the length of the string using string length function
   strcpy(destination,a);
   cout<<"the length of the given string is "<<strlen(destination);

   return 0;
 }


OUTPUT:-



     //ENJOY CODING\\

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post