C ++ Strings: Χρήση πίνακα char και αντικειμένου string

Πίνακας περιεχομένων

Σε αυτό το άρθρο, θα μάθετε να χειρίζεστε συμβολοσειρές στο C. Θα μάθετε να τις δηλώνετε, να τις προετοιμάζετε και να τις χρησιμοποιείτε για διάφορες λειτουργίες εισόδου / εξόδου.

Το String είναι μια συλλογή χαρακτήρων. Υπάρχουν δύο τύποι συμβολοσειρών που χρησιμοποιούνται συνήθως στη γλώσσα προγραμματισμού C ++:

  • Συμβολοσειρές που είναι αντικείμενα κλάσης συμβολοσειρών (Η κλάση συμβολοσειρών Βασικής βιβλιοθήκης Standard C ++)
  • Χορδές C (χορδές τύπου C)

Χορδές C

Στον προγραμματισμό C, η συλλογή χαρακτήρων αποθηκεύεται με τη μορφή συστοιχιών, υποστηρίζεται επίσης στον προγραμματισμό C ++. Ως εκ τούτου ονομάζεται C-string.

Οι C-string είναι συστοιχίες τύπου που charτερματίζονται με μηδενικό χαρακτήρα, δηλαδή (η τιμή ASCII του μηδενικού χαρακτήρα είναι 0).

Πώς να ορίσετε μια συμβολοσειρά C;

 char str () = "C ++";

Στον παραπάνω κώδικα, strείναι μια συμβολοσειρά και περιέχει 4 χαρακτήρες.

Αν και το " C++" έχει 3 χαρακτήρες, ο μηδενικός χαρακτήρας προστίθεται αυτόματα στο τέλος της συμβολοσειράς.

Εναλλακτικοί τρόποι καθορισμού μιας συμβολοσειράς

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

Όπως οι πίνακες, δεν είναι απαραίτητο να χρησιμοποιήσετε όλο το χώρο που διατίθεται για τη συμβολοσειρά. Για παράδειγμα:

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

Παράδειγμα 1: C ++ συμβολοσειρά για να διαβάσετε μια λέξη

Πρόγραμμα C ++ για την εμφάνιση μιας συμβολοσειράς που έχει εισαχθεί από τον χρήστη

 #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.

ενδιαφέροντα άρθρα...