Spread the love

Strings

What Are Strings in C?

In C, strings are sequences of characters such as letters, numbers, or symbols used to represent text. Unlike other languages, strings in C are implemented as arrays of characters. Each character in a string is stored in a specific position within this array.

A unique feature of strings in C is the inclusion of a special character at the end of the array called the ‘null terminator,’ represented by \0. This null terminator signals the end of the string to the computer.

For example, the string “Hello” in C is stored in memory as “Hello\0”.

VIDEO CREDIT:- CODE WITH HARRY

How to Create Strings in C

Creating strings in C involves using arrays of characters. Here are a few ways to create and manipulate strings in C:

  1. Using Character Arrays: You can create a string by defining a character array and initializing it with a sequence of characters.
    char greeting[] = "Hello";

    In this example, the array greeting contains six characters: ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, and the null terminator \0.

  2. Using Pointers: You can also create a string using a pointer to a character.
    char *greeting = "Hello";

    Here, greeting points to a string literal stored in read-only memory, which is automatically null-terminated.

  3. Manual Initialization: You can manually initialize the character array with each character, including the null terminator.
    char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

    This explicitly sets each character in the array, ensuring that the string is properly null-terminated.

Important Considerations

  • Null Terminator: Always ensure that your strings are null-terminated. Without the \0, functions that operate on strings (such as strlen and strcpy) will not work correctly and might read beyond the array boundaries, leading to undefined behavior.
  • String Length: When declaring a character array for a string, make sure it is large enough to hold all the characters plus the null terminator. For example, to store “Hello”, you need an array of at least 6 characters.
  • Modifying Strings: If you need to modify the string, use character arrays rather than pointers to string literals, as string literals are stored in read-only memory.

Example: Creating and Printing a String

Here is a complete example demonstrating how to create and print a string in C:

#include <stdio.h>

int main() {

char greeting[] = "Hello, World!";
printf("%s\n", greeting);
return 0;
}

In this example, the greeting array is initialized with the string “Hello, World!”, and printf is used to print the string to the console. The %s format specifier in printf is used for strings.

How to Manipulate Strings in C

Manipulating strings in C involves various operations like copying, concatenating, comparing, and finding the length of strings. Here are some common string manipulation functions provided by the C standard library:

  1. Copying Strings: strcpy and strncpy
    • strcpy: Copies one string into another.
      #include <stdio.h>

      #include <string.h>
      int main() {
      char source[] = "Hello, World!";
      char destination[50];
      strcpy(destination, source);
      printf("Copied string: %s\n", destination);
      return 0;
      }
    • strncpy: Copies up to a specified number of characters.
      #include <stdio.h>

      #include <string.h>

      int main() {

      char source[] = "Hello, World!";
      char destination[50];

      strncpy(destination, source, 5);

      destination[5] = '\0';
      // Ensure null termination
      printf("Copied string: %s\n", destination);
      return 0;
      }
  2. Concatenating Strings: strcat and strncat
    • strcat: Appends one string to the end of another.
      #include <stdio.h>

      #include <string.h>

      int main() {

      char str1[50] = "Hello";
      char str2[] = ", World!";
      strcat(str1, str2);
      printf("Concatenated string: %s\n", str1);
      return 0;
      }
    • strncat: Appends up to a specified number of characters from one string to another.
      #include <stdio.h>

      #include <string.h>
      int main() {
      char str1[50] = "Hello";
      char str2[] = ", World!";
      strncat(str1, str2, 5);
      printf("Concatenated string: %s\n", str1);
      return 0;
      }
  3. Comparing Strings: strcmp and strncmp
    • strcmp: Compares two strings lexicographically.
      #include <stdio.h>

      #include <string.h>

      int main() {

      char str1[] = "Hello";
      char str2[] = "World";
      if (strcmp(str1, str2) == 0)
      {
      printf("Strings are equal.\n");
      } else {
      printf("Strings are not equal.\n");
      }

      return 0;

      }
    • strncmp: Compares up to a specified number of characters of two strings.
      #include <stdio.h>

      #include <string.h>

      int main() {

      char str1[] = "Hello";
      char str2[] = "Helium";
      if (strncmp(str1, str2, 3) == 0) {
      printf("First 3 characters are equal.\n");
      } else {
      printf("First 3 characters are not equal.\n");
      }

      return 0;

      }
  4. Finding String Length: strlen
    • strlen: Returns the length of a string (excluding the null terminator).
      #include <stdio.h>

      #include <string.h>
      int main() {
      char str[] = "Hello, World!";
      printf("Length of string: %zu\n", strlen(str));
      return 0;
      }

Example: Combining String Manipulation Functions

Here is an example that combines several string manipulation functions:

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

int main() {

char str1[50] = "Hello";
char str2[] = ", World!";
char str3[50];
// Copying str1 to str3
strcpy(str3, str1);
printf("Copied str1 to str3: %s\n", str3);
// Concatenating str2 to str3
strcat(str3, str2);
printf("Concatenated str2 to str3: %s\n", str3);
// Comparing str1 and str3
if (strcmp(str1, str3) == 0) {
printf("str1 and str3 are equal.\n");
} else {
printf("str1 and str3 are not equal.\n");
}
// Finding the length of str3
printf("Length of str3: %zu\n",
strlen(str3));
return 0;
}

This program demonstrates copying, concatenating, comparing, and finding the length of strings using the appropriate standard library functions.

Categories: C

techbloggerworld.com

Nagendra Kumar Sharma I Am Software engineer

0 Comments

Leave a Reply

Avatar placeholder

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