C Strings - JohnHau/mis GitHub Wiki

https://www.programiz.com/cpp-programming/strings

C++ Strings In this tutorial, you'll learn to handle strings in C++. You'll learn to declare them, initialize them and use them for various input/output operations.

String is a collection of characters. There are two types of strings commonly used in C++ programming language:

Strings that are objects of string class (The Standard C++ Library string class) C-strings (C-style Strings) C-strings In C programming, the collection of characters is stored in the form of arrays. This is also supported in C++ programming. Hence it's called C-strings.

C-strings are arrays of type char terminated with null character, that is, \0 (ASCII value of null character is 0).

image

image

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.

image

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.

image

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, '\n');

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; }

image

⚠️ **GitHub.com Fallback** ⚠️