Strings:

String is a primitive data type which we use to store text or sequence of characters. Strings are always enclosed in double quotation marks. String is an array of characters. We will discuss arrays later on.


char name[50] = "Andrew";

  • char is the data type.
  • name is the name of variable.
  • [50] specifies the length of characters in this string variable.

Concatenation:

Concatenation is a method which allows us to join two or more strings. We can use strcat() function for string concatenation.

string.c
#include <stdio.h>
#include <string.h>
  
int main() {
  char greet1[50] = "Hello, ";
  char greet2[50] = "World!";
  
  strcat(greet1, greet2);
  
  printf("Concatenated string: %s", greet1);
  
  return 0;
}

Note: If you have numbers enclosed in quotation marks (string), they will not sum up, rather they will be concatenated.


Accessing Characters in String:

As we know that strings are arrays (store multiple characters), so we can access every character using indexes inside square brackets.
Arrays are data structure which can store multiple data values which have same data type.


string.c
#include <stdio.h>

int main() {
  char name[] = "John"; 

  char firstChar = name[0];
  char thirdChar = name[2]; 

  printf("First character: %c\n", firstChar);
  printf("Sixth character: %c\n", thirdChar);

  return 0;
}

Note: If you want to print double quotes inside string, use \"


string.c
#include <stdio.h>

int main() {
  printf("Hello World!\n");
  printf(""Hello World!"");
  return 0;
}

Inputs in String:

There are two methods of taking inputs in string.

string.c
#include <stdio.h>

int main() {
  char name[50]; 
    
  // fgets consider spaces (recommended)
  printf("Enter your name: ");
  fgets(name, sizeof(name), stdin); 

  printf("You entered: %s", name);
    
  // scan do not consider spaces
  printf("Enter your name : ");
  scanf("%s", name);
  printf("You entered: %s", name);

  return 0;
}

Modification in Strings:

We can modify our whole string or its part. We can access a specified index and can change it.

string.c
#include <stdio.h>

int main() {
  char name[50] = "John";
  name[0] = 'M';
  printf("%s", name);

  return 0;
}

Methods of Strings:

In C, there is no built-in string data type or string methods like in C++ or any other language. We represent string as an array of characters in C. Whenever we want to use methods of strings, we have to include a header file #include <string.h>
MethodDescriptionCode
strlen()It is used to calculate the length of a string variable.
printf("%d", strlen("Hi there!"));
strcat()It is used to concatenate strings.
printf("%s", strcat(str1, str2));
strcpy()It is used to copy one string to the other.
strcpy(str2, str1);
strncpy()It is used to copy one string to the other but allows you to specify the number of characters to copy.
char *strncpy(
  char *dest,
  const char *src,
  size_t n
);
strcmp()It is used to compare two strings. If they are same, it returns true otherwise false.
printf("%d\n", strcmp(str1, str2));
*strstr()It is used for string searching.
char *strstr(
  const char *haystack,
  const char *needle
);