Strings in c

  • We have seen an array of integers.similarly we can also have an array of float numbers. But when we make an array of character i.e. “char” type data, it is termed as a string
  • A string i.e an array of character is normally terminated with a null character to indicate the termination
  • The null character is just used as a reference to indicate the termination of a string. The ASCII value of the null character is 0 (zero). It is written as ‘\0’
  • Hence we can say “an array of characters terminated with a null character is called as the string”
  • An example declaration of a string can be char a[100];
  • If a string “INDIA” is to be stored in this array then it will consume 6 spaces or 6 memory bytes (as we know each character requires 1-byte space in memory). This string will be stored as below;
india image in C
Example of string
  • Whenever the compiler reads a string it inserts a null character at the end
  • If a string is read using the “cin” command, the first blank space is treated as a null character
  • In this case we will not be able to use “cin”,instead, we have to use gets() function(function used to accept a string from the user)
  • To store a string of 5 characters we need an array size of at least 6 characters i.e. 5 characters and 1 null character

HEADER FILE String.h:

String function
string function in C

This header file has many functions that perform a certain operation on strings. These functions make our programming work much simpler.

1. strlen() function

  • This function returns an integer value that is the length of the string passed to the function
  • When returning the length of the string it does not consider the space required for the null character
  • Hence it returns the exact length of the string it does not consider the space required for the null character
  • Hence it returns the exact length of the string neglecting the space required for a null character

Write a program to accept a string and display its length

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int l;
char a[100];
clrscr();
printf("Enter a string\n");
gets(a);
l=strlen(a);
printf("The length of the entered string is:%d",l);
getch();
}

OUTPUT:
Enter a string
Hey
The length of the entered string is: 3

2. Strcpy() Function

  • This function copies the second string into the first string passed to the function
  • The second string remains unchanged. Only the first is changed and gets a copy of the second string.For e.g. strcpy(str1,str2),will copy the string “str2” into the “str1”

Write a program to accept a string, copy it into another string and display this new string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],b[100];
clrscr();
printf("Enter a string\n");
gets(a);
strcpy(b,a);
printf("The new string is %s",b);
getch();
}

OUTPUT:
Enter a string
how are you ?
The new string is how are you ?

3.strcmp() function

  • This function compares the two string variables passed to it. It returns an integer value equal to

1.0 (zero), if the two strings are equal
2. A negative value, if the first string is greater than the second string
3. A positive value, if the first string is greater than the second string

  • Both the string remains unchanged
  • The string is smaller means its alphabetical sequence is smaller. For example, “Hello” is lesser than “Hi”; because the first character “Hi” is the same but the second character “e” is smaller than “i”
  • “e” is smaller than “i” because “e” comes before “i” in the alphabets i.e. A, B, C,…. Z. The function compares the ASCII value of the characters

Write a program to accept two strings, compare them and display if they are equal or not. If they are not equal the one which is greater

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],b[100];
clrscr();
printf("Enter two strings:\n");
gets(a);
gets(b);
if(strcmp(a,b)==0)
printf("The strings are equal");
else if(strcmp(a,b)>0)
printf("%s string is greater",a);
else
printf("%s string is greater",b);
getch();
}
OUTPUT:
Enter two strings:
Hello
Hi
Hi string is greater

4.Strcat() function

  • This function concatenates(joins) the two string variables passed to it. It returns a string of the combination of the two in the first string variable

Write a program to accept two strings, join them and display the result

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
cahr a[100],b[100];
clrscr();
printf("Enter two strings:\n");
gets(a);
gets(b);
strcat(a,b);
printf("The concatenated string is %s",a);
getch();
}

OUTPUT:
Enter two strings:
Abhishek
Verma
The concatenated string is AbhishekVerma

5. Strlwr() Function

  • This function present in the header file string.h
  • It is used to convert the given string into lowercase
  • It is a built-in function in the C which is used to convert the string into lowercase

Write a program using function strlwr()

#include<stdio.h> 
#include<string.h> 
  
void main() 
{ 
    char str[ ] = "FEM-CODE TUTORIAL"; 
  
    // It will converting the given string into lowercase. 
    printf("%s\n",strlwr(str)); 
  
    getch();
} 

OUTPUT:
fem-code tutorial

6. Strupr() Function

  • This built-in function in the C which is used to convert the string into uppercase

Write a program using function strupr()

#include<stdio.h> 
#include<string.h> 
  
void main() 
{ 
    char str[ ] = "fem-code tutorial"; 
  
    // It will converting the given string into lowercase. 
    printf("%s\n",strupr(str)); 
  
    getch();
} 

OUTPUT:
FEM-CODE TUTORIAL

Check whether the string is palindrome or not

#include<stdio.h>
#include<conio.h>
int main()
{
char a[100][100],b[100][100];
printf("enter a string to check if it is a palindrome:\n ");
get(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
{
printf("the string is palindrome.\n");
}
else
{
printf("the string is not palindrome.")
}
}
OUTPUT:
enter a string to check if it is a palindrome:
madam
the string is palindrome.

Translate »