Chaînes C ++: Utilisation d'un tableau de caractères et d'un objet chaîne

Dans cet article, vous apprendrez à gérer les chaînes en C. Vous apprendrez à les déclarer, à les initialiser et à les utiliser pour diverses opérations d'entrée / sortie.

String est une collection de caractères. Il existe deux types de chaînes couramment utilisées dans le langage de programmation C ++:

  • Chaînes qui sont des objets de la classe de chaînes (classe de chaînes de la bibliothèque C ++ standard)
  • Chaînes C (chaînes de style C)

Chaînes C

En programmation C, la collection de caractères est stockée sous forme de tableaux, ceci est également pris en charge dans la programmation C ++. Par conséquent, cela s'appelle C-strings.

Les chaînes C sont des tableaux de type charterminé par un caractère nul, c'est-à-dire (la valeur ASCII du caractère nul est 0).

Comment définir une chaîne C?

 char str () = "C ++";

Dans le code ci-dessus, strest une chaîne et elle contient 4 caractères.

Bien que " C++" ait 3 caractères, le caractère nul est automatiquement ajouté à la fin de la chaîne.

Méthodes alternatives de définition d'une chaîne

char str (4) = "C ++"; char str () = ('C', '+', '+', ' 0'); char str (4) = ('C', '+', '+', ' 0');

Comme les tableaux, il n'est pas nécessaire d'utiliser tout l'espace alloué pour la chaîne. Par exemple:

 char str (100) = "C ++";

Exemple 1: Chaîne C ++ pour lire un mot

Programme C ++ pour afficher une chaîne saisie par l'utilisateur.

 #include using namespace std; int main() ( char str(100); cout <> str; cout << "You entered: " << str << endl; cout <> str; cout << "You entered: "< 

Output

 Enter a string: C++ You entered: C++ Enter another string: Programming is fun. You entered: Programming 

Notice that, in the second example only "Programming" is displayed instead of "Programming is fun".

This is because the extraction operator>> works as scanf() in C and considers a space " " has a terminating character.

Example 2: C++ String to read a line of text

C++ program to read and display an entire line entered by user.

 #include using namespace std; int main() ( char str(100); cout << "Enter a string: "; cin.get(str, 100); cout << "You entered: " << str << endl; return 0; )

Output

 Enter a string: Programming is fun. You entered: Programming is fun. 

To read the text containing blank space, cin.get function can be used. This function takes two arguments.

First argument is the name of the string (address of first element of string) and second argument is the maximum size of the array.

In the above program, str is the name of the string and 100 is the maximum size of the array.

string Object

In C++, you can also create a string object for holding strings.

Unlike using char arrays, string objects has no fixed length, and can be extended as per your requirement.

Example 3: C++ string using string data type

 #include using namespace std; int main() ( // Declaring a string object string str; cout << "Enter a string: "; getline(cin, str); cout << "You entered: " << str << endl; return 0; )

Output

 Enter a string: Programming is fun. You entered: Programming is fun. 

In this program, a string str is declared. Then the string is asked from the user.

Instead of using cin>> or cin.get() function, you can get the entered line of text using getline().

getline() function takes the input stream as the first parameter which is cin and str as the location of the line to be stored.

Passing String to a Function

Strings are passed to a function in a similar way arrays are passed to a function.

 #include using namespace std; void display(char *); void display(string); int main() ( string str1; char str(100); cout << "Enter a string: "; getline(cin, str1); cout << "Enter another string: "; cin.get(str, 100, ''); display(str1); display(str); return 0; ) void display(char s()) ( cout << "Entered char array is: " << s << endl; ) void display(string s) ( cout << "Entered string is: " << s << endl; )

Output

 Enter a string: Programming is fun. Enter another string: Really? Entered string is: Programming is fun. Entered char array is: Really?

In the above program, two strings are asked to enter. These are stored in str and str1 respectively, where str is a char array and str1 is a string object.

Then, we have two functions display() that outputs the string onto the string.

The only difference between the two functions is the parameter. The first display() function takes char array as a parameter, while the second takes string as a parameter.

This process is known as function overloading. Learn more about Function Overloading.

Articles intéressants...