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);

Strrev :- It is called as 'String reverse ' function. It is used to reverse the string.

Syntax :- strrev(string 1);

Strupr :- It is called as 'String uppercase ' function. It is used to convert string in to the uppercase.

Syntax :- strupr(string 1);

Strlwr :- It is called as 'String lowercase ' function. It is used to convert string in to the lowercase.

Syntax :- strlwr(string 1);


Example or Program of string Manipulation :-



     
 // special header file for manipulating any string
#include <string.h>
#include <stdio.h>
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);
   printf("The copied string is %s \n",destination);

   // string for concatinating\joining two strings or string concatination function
   strcat(a, b);
   printf("The concatinated or combined string is %s \n", 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)
   {
   printf("%s is bigger \n",a);
   }
   else
   printf("%s is bigger \n",b);

   // copying variable a to destination and finding the length of the string using string length function
   strcpy(destination,a);
   printf("%d is the length of the given string \n",strlen(destination));

   // Reversing the string using string reverse function
   strrev(destination);
   printf("%s is the reverse string \n",destination);

    // Converting all the characters of the string to uppercase\capitalletters using string uppercase function
   strupr(destination);
   printf("%s is the uppercase of the string \n", destination);

    // Converting all the characters of the string to lowercase\small letters using string lowercase function
   strlwr(destination);
   printf("%s is the lower case of the string \n", destination);

   // As we have reversed our string before now we are reversing it back to original string
   strrev(destination);
   printf("%s is the original string \n",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