LATEST

How to use strings in C / C++

In this tutorial, we will learn about how to use strings in C / C++. We will learn how to define, initialize, and methods in using strings.

This is one of the important knowledge. So you need to study carefully to really master it.

What is strings in C / C++?

Strings are a collection of characters. In C and C++ languages, we have two commonly used strings:

  • The C-Style character strings.
  • The strings slass in C++.

Example: We have a string str[] = {'H', 'e', 'l', 'l', 'o'}.

To better understand, we will learn about how to define a string in C / C++.

How to define strings in C / C++?

To define a string we need to care about the string name, the size of the string and the data type of the string is always char.

Example:

char str[] = "C++";

In the above code, the string str has 4 characters. Includes 3 "C++" characters and a \0 character at the end of the string.

Note: The size of a string is always 1 larger than the number of characters contained in the string. This is the \0 character at the end of each string.

Other ways to define a string:

char str[4] = "C++";
     
char str[] = {'C','+','+','\0'};

char str[4] = {'C','+','+','\0'};

It is not necessary that we use up all the free space of the string. It is possible to declare the size as 100, but only use 4. For example:

char str[100] = "C++";

The C-Style character strings

The C-Style Character String is an array of characters terminated with the \0 character. It originated in the C language and continues to be supported in the C++ language.

Let us take an illustrative example for you to understand easily.

Example: Let's say we have a string str consisting of the characters "Hello".

If you follow the rule of array initialization, then you can write the above statement as follows:

char str[] = "Hello";

However, it actually has an extra \0 character at the end like this:

char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};

That means, when we define the C-Style character strings, we need to subtract a size to store the \0 character.

Now, we will perform string definition and display in C and C++ programs.

C++ programming:

#include <iostream>
using namespace std;

int main()
{
    char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    cout << "String str: ";
    cout << str << endl;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
  printf("String str: %s", str);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output

strings 01 PNG

Functions that support string processing in C / C++.

Function Purpose
strcpy(s1, s2) Copies string s2 into string s1
strcat(s1, s2) Concatenates string s2 onto the end of string s1
strlen(s1) Returns the length of string s1
strcmp(s1, s2)

Returns:

  • 0 if s1 and s2 are the same
  • Less than 0 if s1<s2
  • Greater than 0 if s1>s2
strchr(s1, ch) Returns a pointer to the first occurrence of character ch in string s1
strstr(s1, s2) Returns a pointer to the first occurrence of string s2 in string s1

*Note: To use the above functions, we need to declare the cstring library.

Here is an example using some of the functions mentioned above.

Example:

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
  char src1[100] = "Hello learnnc.com.";
  char src3[100] = " Learn programming for free";
  char src2[100];
  strcpy(src2,src1);
  cout <<"\nstrcpy(src2, src1) = "<< src2;
  strcat(src1, src3);
  cout<<"\nstrcat(src1, src3) = "<<src1;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  char src1[100] = "Hello learnnc.com.";
  char src3[100] = " Learn programming for free";
  char src2[100];
  strcpy(src2,src1);
  printf("\nstrcpy(src2, src1) = %s",src2);
  strcat(src1, src3);
  printf("\nstrcat(src1, src3) = %s",src1);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

strings 02 PNG

The strings slass in C++

The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. That is the string library.

To use it, we need to declare the string library as follows:

#include <string>

Example:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   string str1 = "Hello";
   string str2 = ", I'm learnnc.com.";
   string str3;
   int  len ;

   // copy str1 into str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // concatenates str1 and str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // total length of str3 after concatenation
   len = str3.size();
   cout << "str3.size() :  " << len << endl;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

Output:

strings 03 PNG

Examples of strings in C / C++

Here, we give some examples using strings in C / C++.

Example 1: Import and export string in C / C++.

C++ programming:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   string str1 = "Hello";
   string str2 = "I'm learnnc.com.";
   string str3 = "Learning C and C++ free.";

   cout << "str1: " << str1 << endl;
   cout << "str2: " << str2 << endl;
   cout << "str3: " << str3 << endl;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
   char str1[100] = "Hello";
   char str2[100] = "I'm learnnc.com.";
   char str3[100] = "Learning C and C++ free.";

   printf("str1: %s\n",str1);
   printf("str2: %s\n",str2);
   printf("str3: %s\n",str3);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

strings 05 PNG

Example 2: Program to concatenate two strings in C / C++.

C++ programming:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   string str1 = "Hello";
   string str2 = ", I'm learnnc.com.";
   string str3;

   // concatenates str1 and str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
   char str1[100] = "Hello";
   char str2[100] = ", I'm learnnc.com.";

   // concatenates str1 and str2
   strcat(str1, str2);
   printf("str1 + str2 : %s\n",str1);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

strings 04 PNG

Cùng chuyên mục:

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

Searching and sorting arrays in C / C++

Searching and sorting arrays in C / C++

How to use arrays in C / C++

How to use arrays in C / C++

How to use goto statement in C / C++

How to use goto statement in C / C++

How to use switch...case statement in C / C++

How to use switch...case statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use if - else statement in C / C++

How to use if - else statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use do while loop in C / C++

How to use do while loop in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

How to use for loop in C / C++

How to use for loop in C / C++

Top