String in C programming4 min read

A string in C programming can be defined as a group of characters or an array of characters i.e. a string is any valid variable name and is always declared as an array.

Syntax: char string_name[size]

The size determines the number of characters that are held by variable string_name. When the compiler assigns a character string to a character array it automatically supplies s null character (0) at the end of the string. Therefore, the size of the array of character data types is calculated as

Array_size = Maximum number of characters in the string + 1

Each character in a string can be accepted one by one i.e. just like elements in arrays.

  • char name[10] “HIBERSTACK”;
  • OR char name[0] ={ ‘H’, ‘I’, ‘B’, ‘E’, ‘R’, ‘S’, ‘T’, ‘A’, ‘C’, ‘K’, ‘\0’ };

8.2 String Handling Functions

‘C’ language supports various string handling functions and is given below. In order to use them, it is necessary to include header file <string.h>

1) strcat() : Concatenates two strings. | Addition of characters including blank space is known as Concatenation.

syntax : strcat (string 1, string 2);

void main(void)

{

  char string 1 [25] = "Data"; 
  strcat(stringl," Structures");
  printf("%s",stringl);
  
}

On execution:

Data Structures

2) strlen ( ): Find the length of a string including blank space

syntax: strlen (string);

void main(void)

{

  char string [50] = "Data Structures"; 
  printf("%d",strlen(string));
  
}

On execution:

15

3) strepy(): Copies one string over another

syntax : strcpy(string1, string2);

| Ex.

void main(void)

{

  char string1[50]="Hello"; 
  char string2 [20] = "Friends"; 
  strcpy(string1, string2);
  printf("%s\n%s",stringl, string2);

}

On execution:

4) strcmp() : Compare two string by the argument and has a value 0 if they equal (case – sensitive).

syntax: strcmp(string1, string2);

void main(void)

{

  char string1 ="KISHORT";
  char string2 = "kishor"; 

  if(strcmp((string1, string2)==0)
   
  {

    printf("strings are equal");
  else
    printf("strings are not equal");
 
  }

On execution: strings are not equal.

5) strempi : -Compare two string without case sensitive i.e. ignore case letter.

syntax: strempi (string1, string2):

void main(void) 

{ 
  if (strempi ( "Ig", "LG") 
    printf("Strings are equal ");
  else
    printf("Strings are equal ");
}

6) strlwr : Convert string to lowercase.

syntax: strlwr(string1);

void main(void) 

{
  
  char a[3] = "LG", b[3];
  strcpy (b, a);
  strlwr (a);
  printf("Original = %s Lower=%", b, a);
 
}

7) strupr : Convert string to uppercase

syntax: strupr ( string1);

8) strrev : Reverse the string.

syntax: strrev(string1);

A program to check string is palindrome or not A string is said to be palindrome if original string and its reverse spell exactly.

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

void main(void) 

{

  char ori[30],temp[30]; 
  clrscr(); 
  
  printf (" Enter a string :- "); 
  gets(ori); 
  strcpy(temp, ori);
  strrev(temp);
  
  if( strcmpi (ori, temp) ==0)
  
  {
  
  /* Copy the original string into temp variable/ 
  /* Reverse the contents of temp variable */
  
  printf("\n String %s is Palindrome",ori); 
  
  else
    
    printf(" \n String %s is not Palindrome".ori); 
    
  getch(); 
  
}

A program to count number vowels and consonants in a given string

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

#include<ctype.h> 
void main(void) 

{ 

  char str[30];
  int a, e, i, o, u, V, c, 1, j; 
  clrscr(); 
  
  printf(" Enter a string :- "); 
  gets(str); 1 = strlen(str);
  
  /* Total length of string */ 
  
  a = e = i = 0 = u = 0; 
  
  /* Inilization */ 
  
  for(j = 0; j<1; ++j) 
  
  {
  
    switch(tolower(str[j])) 
    
    { 
    
      case 'a':
  /* folower() convert the character to lower case */
  
  ++a;
  break; case 'e':
  ++e;
  break; case 'i' :
  Hi;
  break; case 'o':
  ++o;
  break; case 'u' :
  Hu;
  
  printf("\n\n Total a = =%d\n",a); 
  printf(" Total e = %d\n",e); 
  printf(" Total i = %d\n",1); 
  printf(" Total o = %d\n",0); 
  printf(" Total u = %d\n\n",u); 
  
  v = a +e+ i + 0 + u;
  
  printf(" Total vowels = %d\n\n",v); 
  
  c = 1 - V;
  
  printf(" Total Consonants = %d",c); 
  
  getch();
  
}

A program to print the following

#include<stdio.h> 
#include<conio.h> 
#include<string.h> 
void main(void) 

{

  char a[]="ABCDEFEDCBA"; 
  int i,len,h; 
  clrscr(); 
  
  len = strlen(a);
  h= len/2; 
  
  printf("%s\n",a); 
  
  for(i=0;i<=h;Hi)
  
  {
  
  a[h-i] = a[h+i] = '';
  
  printf("%s\n",a); 
  
  } 
  
  getch();
  
}

Share:

Leave a Reply