String in C programming is a sequence of characters with a null character ‘\0’. Strings are defines as an array of characters is knows as Strings. Strings are actually one-dimensional array.

Declare

There are two ways to declare a string in c language.

  • By char array
  • By string literal

The example of declaring string by char array in C language.

char ch[12]={‘b’, ‘a’, ‘s’, ‘i’, ‘c’, ‘e’, ‘n’, ‘g’, ‘i’, ‘e’, ‘e’, ‘r’, ‘\0’};

 Array index starts from 0, so it will be represented as in the figure

String Example in C

Input Value

#include<stdio.h>  
#include <string.h>    
int main(){    
  char h[12]={'b', 'a', 's', 'i', 'c', 'e', 'n', 'g', 'i', 'e', 'e', 'r', '\0'};    
   char r[12]="basicengieer";    
    
   printf("Char Array : %s\n", h);    
   printf("String Literal : %s\n", r);    
 return 0;    
}  

Output Value

Output value

Traversing String

Traversing the string is one of the most important aspects in any of the C programming language. It is somewhat different from the traversing an integer array.

There are two ways to traverse a string

  • Using the length of string.
  • Using the null character.

Using the length of string

Example

input value

#include<stdio.h>  
void main ()  
{  
    char s[13] = "Basicengieer";  
    int i = 0;   
    int count = 0;  
    while(i<13)  
    {  
        if(s[i]=='a' || s[i] == 'i' || s[i] == 'e' || s[i] == 'i' || s[i] == 'e' ||s[i] == 'e')  
        {  
            count ++;  
        }  
        i++;  
    }  
    printf("The number of vowels %d",count);  
}  

Output value

output value

Using the null character

Example

Input value

#include<stdio.h>  
void main ()  
{  
    char s[13] = "Basicengieer";  
    int i = 0;   
    int count = 0;  
    while(s[i] != NULL)  
    {  
        if(s[i]=='a' || s[i] == 'i' || s[i] == 'e' || s[i] == 'i' || s[i] == 'e' ||s[i] == 'e')  
        {  
            count ++;  
        }  
        i++;  
    }  
    printf("Number of vowels %d",count);  
}  

Output Value

Output value

C gets() and puts() functions

It is declare in the header file stdio.h. Both the functions are involve in the i/o operations of the strings.

Gets() function

gets() function is define in the <stdio.h> header file of C. C library also facilitate a special function to read a string from a user is knows as Get() function. This function is represent as gets() function.

Syntax

  • char[] gets(char[]); 

Example

Input value

#include <stdio.h>

int main () {
   char rom[20];

   printf("Enter a string : ");
   gets(rom);

   printf("You enter: %s", rom);

   return(0);
}

Output Value

Output value

Puts() Function

puts() function is define in the <stdio.h> header file of C. C library also facilitate a special function to print a string on the console screen is knows as puts() function. This function is represent as puts() function.

Syntax

  • int puts(char[]);

Example

Input value

#include<stdio.h>  
#include <string.h>    
int main(){    
char string[50];    
printf("Enter a string: ");    
gets(string);     
printf("you enter: ");    
puts(string);      
return 0;    
}    

Output Value

Output value

C String Functions

FunctionDescription
strcpyThe copy the source string to destination string.
strcatIt is a concatenates first string with second string.
strlenThe returns the length of string name.
strcmpIt is a compares the first string with second string.
strlwrString characters in lowercase.
struprString characters in Uppercase.

strcpy Function

Example

Input value

#include<stdio.h>  
#include <string.h>    
int main(){    
 char r[15]={'b', 'a', 's', 'i', 'c', 'e', 'n', 'g', 'i', 'e', 'e', 'r', '\0'};    
   char t[15];    
   strcpy(t,r);    
   printf("second string : %s",t);    
 return 0;    
}    

Output value

Output value

strcat Function

Example

Input value

#include<stdio.h>  
#include <string.h>    
int main(){    
  char y[15]={'b', 'a', 's', 'i', 'c', 'e', 'n', 'g', 'i', 'e', 'e', 'r', '\0'};    
   char u[15]={'h', 'e', 'l', 'l', 'o', '\0'};    
   strcat(y,u);    
   printf("Value of first string is: %s",y);    
 return 0;    
}    

Output value

Output value

strlen Function

Example

Input value

#include<stdio.h>  
#include <string.h>    
int main(){    
char ch[20]={'b', 'a', 's', 'i', 'c', 'e', 'n', 'g', 'i', 'e', 'e', 'r', '\0'};
   printf("Length of string is: %d",strlen(ch));    
 return 0;    
}    

Output value

Output value

strcmp Function

Example

Input value

Output value

unequal Output value

Unequal Output value

Equal output value

Equal output Value

strlwr

Example

Input value

#include<stdio.h>  
#include <string.h>    
int main(){    
  char rom[52];    
  printf("Enter string: ");    
  gets(rom);      
  printf("\nLower String is: %s",strlwr(rom));    
 return 0;    
}    

Output value

strupr Function

Example

Input value

#include<stdio.h>  
#include <string.h>    
int main(){    
  char rom[52];    
  printf("Enter string: ");    
  gets(rom);      
  printf("\nUpperCase String is: %s",strupr(rom));    
 return 0;    
}    

Output value

Output value

strrev Function

Example

Input Value

#include<stdio.h>  
#include <string.h>    
int main(){    
  char x[58];    
  printf("Enter string: ");    
  gets(x);     
  printf("\nReverse String is: %s",strrev(x));    
 return 0;    
}  

Output value

Output value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about C Programming language please Wikipedia click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *