echo '' ;

C Library – string.h

The C Library string.h provides various functions for handling strings in C programming. It includes functions for string manipulation, comparison, copying, concatenation, and searching. Some commonly used functions in string.h include strlen() for determining the length of a string, strcpy() for copying strings, strcat() for concatenating strings, and strcmp() for comparing strings. Additionally, string.h provides functions for searching for substrings within a string, such as strchr() and strstr(). The library also offers functions for converting strings to other data types and vice versa, like atoi() and itoa(). Overall, string.h is a crucial part of the C standard library, providing developers with efficient and convenient tools for working with strings in C programs. Its functions are widely used in various applications, from basic string manipulation to complex text processing tasks.

Read more: C Library – string.h

String Understanding

  • String is a collection of characters in between the double quotation “.
  • In C language does not have a data type like string, so we can get this from array of characters.
  • Last character of sting should be null characters ‘/0’.
  • Generally compiler will add the null characters when read a string from computer.
  • Dose not appear null characters ‘/0’ when print the string in computer display.
  • Data length is number of characters in the string with null characters.
  • string handling functions strlen() , strcpy()  , strcat()  , strcmp()  , strrev()  , strupr()   , strlwr()  , strrev()  ,

Declaring string

  • Syntax – char string_name[size];
  • size-maximum number of characters including ‘0/’.
  • Example : char city [10];

Initializing string

  • Syntax : static char variable_name [size] = str;
  • Example-1: static char city[] = “ArunEworld”;
  • Example-2: static char city[10] = “ArunEworld”;
  • Example-3: static char city[10] = {‘A’,’r’,’u’,’n’,’E’,’w’,’o’,’r’,’l’,’d’};

Reading string

Reading can be done by pre-defined library functions using scanf(), getchar() and gets().

FunctionExample
scanf()char name[50];
scanf(%s”, name);
getchar()char c, name[50];
int i=0;
while((c = getchar())!=’0/’); name[i++] = c;
name[i] = ‘\0’;
gets()char name[50];
gets(name);

Printing string

Reading can be done by pre-defined library functions using printf() and puts().

FunctionExample
printf()printf(“%s”, var_name);
puts()puts(var_name);


string.h Library Functions

The followings are string.h library functions

FunctionsDescriptions
strlen()calculates the length of string
strcpy()Copies a string into another
strcmp()Compares two strings
strrev()Reverse the given string
strupr()Convert to upper case of given string
strlwr()Convert to lower case of given string
strcat()Appends one string at the end of another

strlen()

Syntax ; int strlen(char *string);

strcpy()

  • Syntax : void strcpy(char *string_!, char *string);
  • functions: strcpy(destination, source);

strcmp()

  • Syntax : int strcmp(char *string_1, char *String_2);
  • strcmp return 0 (Zero) if both the strings are equal , else return -1

Example:

int main()
{
	int a = strcmp("String_One", "String_One");
	printf("%d",a);
	return 0;
}

/*Output: 0 */

strrev()

  • syntax: char *strrev(char *string_1);

strupr()

  • syntax : char* strupr(char *string_1);

strlwr()

  • syntax : string *strlwr(char *string_1);

strcat()

  • Syntax : char *strcat(char *dest, const char *src);
  • syntax ; void strcat( char *string_1, char *string_2);
  • The strcat() function is defined in <string.h> header file.

Example

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

int main(void)
{
    char str1[] = "Arun", str2[] = "Eworld";

    //concatenates str1 and str2 & resultant string stored in str1.
    strcat(str1,str2);

    puts(str1);    
    puts(str2); 

     return 0;
}

/* Output : 
ArunEworld
Eworld 
*/

Examples

String Concatenate (Joint) without strcat() function

Using pointer in while loop.

#include <stdio.h>

int main(void) {
  char A[50] = "Arun";
  char B[50] = "Eworld";
  char *C = A;
  char *D = B;

  while(*C)
  {
    C++;
    
  }
  while(*D)
  {
    *C=*D;
    C++;
    D++;
    printf("%s \n", A);
  }

  *C = '\0';
  printf("%s \n", A);
     return 0;
}

/*Output : ArunEworld"

Using pointer in for loop.

#include <stdio.h>
int main()
{
    char s1[100], s2[100], i, j;

    printf("Enter first string: ");
    scanf("%s", s1);

    printf("Enter second string: ");
    scanf("%s", s2);

    // calculate the length of string s1
    // and store it in i
    for(i = 0; s1[i] != '\0'; ++i);

    for(j = 0; s2[j] != '\0'; ++j, ++i)
    {
        s1[i] = s2[j];
    }

    s1[i] = '\0';
    printf("After concatenation: %s", s1);

    return 0;
}
/* Output : 
gcc version 4.6.3
   
Enter first string:  Arun
Enter second string:  Eworld
After concatenation: ArunEworld   
*/


Find the length of the string without strlen() function.

  • Find the given string length without using strlng()
  • Find the given sting length using while/for loop

Using For Loop

#include <stdio.h>
int main()
{
    char s1[100],i, j;

    printf("Enter string: ");
    scanf("%s", s1);

    // calculate the length of string s1
    // and store it in i
    for(i = 0; s1[i] != '\0'; ++i);
   
    printf("String length is : %d", i);

    return 0;
}


/* Output : 
gcc version 4.6.3
   
Enter string:  ArunEworld
String length is : 10  
*/

Using while loop and pointer

#include <stdio.h>
int main()
{
  char Name[] = "ArunEworld";
  unsigned char count=0;
  char *ptr = Name;
  while(*ptr)
  {
    count++;
    ptr++;
  }
  
  printf("String Length count : %d", count);
  return 0;
}
/*
output
String Length count : 10   
*/

NEXT

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading