7. Strings

Introduction to String

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
The null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word .

How to initialize strings?

You can initialize strings in a number of ways.
  • char c[] = "abcd";
  • char c[50] = "abcd";
  • char c[] = {'a', 'b', 'c', 'd', '\0'};
  • char c[5] = {'a', 'b', 'c', 'd', '\0'};


How to read a line of text?
You can use the fgets() function to read a line of string. And, you can use puts() to display the string.

Example:-

#include <stdio.h>
int main()

{char name[30];
    printf("Enter name: ");
    fgets(name, sizeof(name), stdin);  // read string
    printf("Name: ");
    puts(name);    // display string
    return 0;
}
Output:-
      Enter name: C language
       Name: C language

String Function:-
1). strlen():-The strlen() function takes a string as an argument and returns its length. The returned value is of type size_t (the unsigned integer type).
It is defined in the <string.h> header file.

Example:-

#include <stdio.h>
#include <string.h>
int main()
{
   char a[20]="Program";
    char b[20]={'P','r','o','g','r','a','m','\0'};
    printf("Length of string a = %zu \n",strlen(a)); 
          // using the %zu format specifier to print size_t
    printf("Length of string b = %zu \n",strlen(b));
     return 0;

}

Output:-
                Length of string a = 7
                Length of string b = 7
2).strcpy():- The strcpy() function copies the string pointed by source (including the null character) to the destination.
The strcpy() function also returns the copied string.
The strcpy() function is defined in the string.h header file.

syntax:-

     char* strcpy(char* destination, const char* source);

 Example:-
#include <stdio.h>
#include <string.h>
int main() 
{
    char str1[20] = "C programming";
    char str2[20];
    strcpy(str2, str1);   // copying str1 to str2
     puts(str2);       // C programming
     return 0;
}

Output:- C programming

3). strcmp():- The strcmp() compares two strings character by character.If the first character of two strings is equal, the next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '\0' is reached.

It is defined in the string.h header file.

Return Value from strcmp():-

  • return 0:- if both strings are identical (equal).

  • return negative integer:- if the ASCII value of the first unmatched character is less than the second.

  • return positive integer:-if the ASCII value of the first unmatched character is greater than the second.

4). strcat():- The strcat() function concatenates the destination string and the source string, and the result is stored in the destination string.
Syntax:-
                char *strcat(char *destination, const char *source)

Example:-
#include <stdio.h>
#include <string.h>
int main() {
   char str1[100] = "string in ";
   char str2[] = "c language";
              // concatenates str1 and str2
             // the resultant string is stored in str1.
   strcat(str1, str2);
   puts(str1);
   puts(str2);
    return 0;
}

Output:-
            String in c language
             c language

Post a Comment

0 Comments