String Manipulation



String Manipulation

What is a String: String is a collection of character set which may meaning full or may not, collection of character is called String.

example: India, America, Ame1123rica, 123India, 12654.

The upper characters set are all string in case of "Ame1123rica" 1123 is numeric value but when we write them as a string they are treated as String. "12654" is also a string   because when we write anything in the double invited comma then all thing is treated as string and "12654" is not able to numeric  operation as they are string. In C language we represent the string in between double invited comma.

                    Requirement of String manipulation in programmning

Sting manipulation is a very important part of programming in the programming we need to manipulation a string like reverse a string, concatenate the two string, check a string is palindrome or not, find the frequency of a string, compare the two string, find the length of two string etc.


Sample code of some string manipulation program:

  1. Write a program to find a length of a given string.
  2. write a program to convert a string lowercase to uppercase.
  3. Write a program to palindrome checking.
  4. Write a program to show the  ASCII value of the given string.
  5. Write a program to find a particular character sequence in a given string.
  6. Write a program to convert a string to the opposite case.
  7. Write a program to convert a string to sentence case.
  8. Write a program  to compare two string.
  9. Write a program to find the lengthy string.
  10. Write a program to concatenation two string.
  11. Write a program to swap two string.
  12. Write a program to find the frequency of a string(count the characters uniquely). .
1.QUESTION: Write a program to find a length of a given string.
CODE:
/* TO FIND A LENGTH OF A GIVEN STRING */
 # include<stdio.h>
# include<conio.h>
# include<string.h>
int main()
{
char Str[20];
int len;
printf("Enter your string: ");
gets(Str);
len = strlen(Str);
printf("\nThe length of %s is %d",Str,len);
return 0;
}

 OUTPUT: 


DISCUSSION:     Here the program where we find the length of a given string, length means how many character's used to construct the string. we use a library function(in built)called "strlen" which is used to find the length of a given string. String is a collection of character's and space is also a character, so the length of a string is the summation of all characters also include the space.

 2. write a program to convert a string lowercase to uppercase.

CODE:
/* CONVERT A STRING LOWER CASE TO UPPER CASE */
# include<stdio.h>
# include<conio.h>
# include<string.h>
int main()
{
char Str[20];
int i = 0,a;
printf("Enter your string: ");
gets(Str);
  while(Str[i]!='\0')
{
if(Str[i]==32)
++i; 
  a = Str[i] - 32;
Str[i] = a;
i++;
}
printf("\nThe new string is: %s",Str);
return 0;

}

OUTPUT:


DISCUSSION:     String is a collection of character's and every character's have a unique ASCII code so the ASCII code of lower case(small letter) is start from 97 = a, 98 = b and so on and the upper case (capital letter) is start from 65 = A, 66 = B and so on . The difference between the uppercase and lowercase is 32 so if we want to convert the character's upper to lower then we need to add just 32 to every character's and we get our required result.

3. Write a program to palindrome checking.
code:
/*PROGRAM TO CHECK PALINDROM*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String[50];
    int i,j=0,length=0,flag=0;
    printf("Enter your string: ");
    gets(String);
    length = strlen(String);
    j = length;
    --j;
    for(i = 0;i<length;i++,j--)
    {
          if(String[i] != String[j])
          {
                       flag = 1;
                       break;
          }
    }
    if(flag == 1)
            printf("\n%s is not palindom",String);
    else
            printf("\n%s is palindom",String);
 
    return 0;
}

OUTPUT:



DISCUSSION:   Palindom is a situation when a word is exact like from start to end and end to start the we call the word is palindom.  In this program we just check the letters is from start to end is same or not if the letters are same then we can say the word is palindom and if the letters are not matched the word is not palindom.
  1. Write a program to show the  ASCII value of the given string.
CODE:
/*PROGRAM TO SHOW THE ASCII VALUE OF EVERY CHARACTER'S OF A STRING */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String[50];
    int i,j=0,length=0,flag=0;
    printf("Enter your string: ");
    gets(String);
    length = strlen(String); 
    for(i = 0;i<length;i++)
    {
    printf(" %d",String[i]);
    }
    getch();
    return 0;
}

OUTPUT:



DISCUSSION:     Every character's have a unique ASCII value so if we represent them in integer type the we can get the ASCII value easily.

3.  Write a program to find a particular character sequence in a given string.

CODE:



4.  Write a program to convert a string to the opposite case.

CODE:
/* PROGRAM TO CONVERT A STRING TO OPPOSITE CASE */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String[50];
    int i=0,j=0,length=0,flag=0;
    printf("Enter your string: ");
    gets(String);
    
    while(String[i]!='\0')
    {
    if(String[i]>90)
printf("%c",String[i]-32);
else
printf("%c",String[i]+32);
i++;
    }
    return 0;
}

OUTPUT:



DISCUSSION:   In this program we convert a given string to its equivalent opposite case if the user input small letters then the program will show capital letter and if the user input capital letter then the program will show all the letters in small letter. We just check if the given characters greater than 90 or not, if the character is greater than 90 then we have to subtract 32 from the character and we get the same character in capital letter and in case the given string is less than 90, that means the number is in capital letter and we need to add 32 and we get the small letter of same character.


Write a program to convert a string to sentence case.

code:
/* PROGRAM TO CONVERT A STRING TO SENTENCE CASE */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String[50];
    int i=0,j=0,length=0,flag=0;
    printf("Enter your string: ");
    gets(String);
    if(String[i]>90)
    String[i]=String[i]-32;
    while(String[i]!='\0')
    {
    if(String[i]==46)
    {
    i++;
    if(String[i]>90)
    String[i]=String[i]-32;
    }    
    i++;
    }
    puts(String);
    return 0;
}


OUTPUT:


DISCUSSION:     In this program can convert a normal string to sentence cases 




4.  Write a program  to compare two string.

CODE:
/* PROGRAM TO COMPARE TWO STRING */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String1[100],String2[100];
    int i=0,j=0,flag=0;
    printf("Enter your string1: ");
    gets(String1);
    printf("Enter your string2: ");
    gets(String2);
    while((String1[i]!='\0')|| (String2[i]!='\0'))
    {
    if(String1[i]!=String2[i])
    {
    flag = 1;
    break;
   
    }
    i++;
    }
    
    if(flag==1)
    printf("\nthe string are not same");
    else
    printf("\nBoth string are same");
    return 0;
    }

OUTPUT:


DISCUSSION:




5.  Write a program to find the length of a string without using any library function.

CODE:
/* PROGRAM TO FIND THE LENGTH STRING WITH OUT USING LIBRARY FUNCTION */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String[100];
    int i=0,j=0,flag=0;
    printf("Enter your string1: ");
    gets(String);
    while(String[i]!='\0')
    i++;

printf("The length of the string is : %d",i)   ;
return 0;
}
 
    OUTPUT:


DISCUSSION:



Write a program to concatenation two string.

CODE:
/*PROGRAM TO CONCATINATE TWO STRING*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String1[100],String2[100],String3[100];
    int i=0,len=0;
    printf("Enter your string1: ");
    gets(String1);
    printf("Enter your string2: ");
    gets(String2);
    strcpy(String3,String1);
    len = strlen(String3);
    while(String2[i]!='\0')
    {
    String3[len] = String2[i];
    i++;
    len++;
}
   
    printf("\nthe new string is: ") ;
    puts(String3);
 
    return 0;
}
 

OUTPUT:

DISCUSSION:




Write a program to swap two string.

CODE:
/* PROGRAM TO SWAP TWO STRING */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char String1[100],String2[100],String3[100];
    int i=0,len=0;
    printf("Enter your string1: ");
    gets(String1);
    printf("Enter your string2: ");
    gets(String2);
    strcpy(String3,String1);
    strcpy(String1,String2);
    strcpy(String2,String3);
 
    printf("\nString1 is: ");
puts(String1);
printf("\nString2 is: ");
puts(String2);
return 0;
}
 
OUTPUT:





Write a program to find the frequency of a string(count the characters uniquely). .

CODE:
/* PROGRAM TO FIND THE NUMBER OF CHARACTER'S OF A STRING */
#include<stdio.h>
#include<conio.h>
int main()
{
char Str[50],sa[52];
int i,j,ca[52];

printf("Enter your string: ");
gets(Str);
for(i = 0;Str[i]!='\0';i++){
ca[i] = 0;
for(j = 0;Str[j]!='\0';j++){

if(Str[i]==Str[j]){
sa[i] = Str[j];
ca[i]++;
}
}
}

for(i = 0;Str[i]!='\0';i++){
printf("\n %c = %d",sa[i],ca[i]);
}
return 0;
}

OUTPUT:








DISCUSSION:










Translate