C String - JohnHau/mis GitHub Wiki

https://codescracker.com/cpp/cpp-strings.htm

Strings are the combination of characters. In other words, you can think strings as an array of characters.

Declaring Strings in C++ Here is the general form to declare strings in C++.

char string_name[string_size]; Here char is valid C++ type, string_name is the name of the string and string_size is the size of the string. The string_size tells, how many character that the string can hold. Here is an example.

char str[20]; The above declaration tells the compiler, that a string str is declared, which can hold upto 20 characters.

Initializing Strings in C++ Here is the general form to initialize strings in C++.

char string_name[string_size] = {comma_separated_character_list}; Here is an example initializing strings in C++.

char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; The above declaration and initialization creates a string that consist the word "Hello". To hold the null character ('\0') at the end of the array, the size of the character array having the string is one more than the number of characters in the word "Hello.". If you adopt the rules of array initialization, then you can write the above statement as follows:

char str[] = "Hello"; Actually, you don't place the null character ('\0') at the end of a string constant. The C++ compiler automatically places the '\0' (null character) at the end of the string when it initializes the array. Let's try to print above-mentioned string:

image

image

image

image

image

Let's take one more example for complete understanding on C++ strings:

/* C++ Strings - Example Program of C++ Strings */

#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main() { clrscr(); char pass[20], check_pass[20], ans; int count_spec_char=0; int count_upper_char=0, count_lower_char=0; int count_num=0, pass_len, i; cout<<"Create a password: "; cin>>pass;

pass_len = strlen(pass); for(i=0; i<pass_len; i++) { if(pass[i]>=65 && pass[i]<=90) { count_upper_char++; } else if(pass[i]>=97 && pass[i]<=122) { count_lower_char++; } else if(pass[i]>=48 && pass[i]<=57) { count_num++; } else { count_spec_char++; } } cout<<"\nThe length of the password is "<<pass_len<<" containing"; cout<<"\n"<<count_upper_char<<" uppercase letter, "<<count_lower_char; cout<<" lowercase letter,\n"<<count_num<<" numbers, and "; cout<<count_spec_char<<" special characters";

cout<<"\n\nWant to change ? (y/n) "; cin>>ans; if(ans=='y' || ans=='Y') { cout<<"Create a new password: "; cin>>pass; } cout<<"\nPassword saved successfully..!!\n"; cout<<"Want to proceed ? (y/n) "; cin>>ans; if(ans=='y' || ans=='Y') { cout<<"\nEnter password: "; cin>>check_pass; if(!strcmp(check_pass, pass)) { cout<<"\nWelcome to codescracker.com\n"; cout<<"This is C++ Strings Tutorial\n"; } else { cout<<"Sorry..!!..Wrong password..\n"; cout<<"Press any key to exit...\n"; getch(); exit(1); } } cout<<"\nPress any key to exit...\n"; getch(); }

image

image

image

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