- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ String
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:
C++ String Examples
Here are some example programs demonstrating the concept of strings in C++ practically.
/* C++ Strings - Example Program of C++ Strings */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char greet[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; cout<<greet<<endl; getch(); }
Here is the sample output of the above C++ program:

Here is another C++ strings example program.
/* C++ Strings - Example Program of C++ Strings */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char str[40]; cout<<"Enter your name: "; cin>>str; cout<<"Hello, "<<str; getch(); }
Below is the sample run of this C++ program.

Here is one more C++ strings example program.
/* C++ Strings - Example Program of C++ Strings */ #include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); char str[80]; int len; int countword=0, i; cout<<"Enter about yourself in one line: "; cin.getline(str, 80); len=strlen(str); for(i=0; i<len; i++) { if(str[i] == ' ') { countword++; } } cout<<"\nAbout Us:\n\t"<<str<<endl; cout<<"\nTotal Number of Words:\n\t"<<countword+1<<" words"; getch(); }
Below is the sample run of the above C++ program:

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(); }
Here are some sample runs of the above C++ program:





More Examples
Here are the list of some more examples on C++ strings:
- Print String
- Find Length of String
- Compare two String
- Copy String
- Concatenate String
- Reverse String
- Delete Vowels from String
- Delete Words from String
- Count Character in String
- Count Word in String
- Remove Spaces from String
- Sort a String
- Convert Uppercase String to Lowercase
- Convert Lowercase String to Uppercase
- Swap two Strings
- Check Strings are Anagram or Not
Practice More Examples on C++ Strings
Here are list of some C++ string programs with output, helps you to understand string in C++ more clearly.
Here is another program on C++ string:
/* C++ String - Example Program */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char arr[26], string[26]; int i, j; // initializing array with values from 65 to 91 for(i=65, j=0; i<92; i++, j++) { arr[j] = i; } // converting and initializing array's values to string // like arr[0] to string[0]. That is, 65 to string[i]. Means, // 'A' to string[i] and so on. for(i=0; i<26; i++) { string[i] = arr[i]; } // printing all the elements of the string[] for(i=0; i<26; i++) { cout<<string[i]; } getch(); }
Following is the sample output of this C++ String example:

Let's take another program demonstrates string in C++:
/* C++ String - Example Program */ #include<iostream.h> #include<string.h> #include<conio.h> void main() { clrscr(); char string[100]; int i, len; cout<<"Enter a sentence\n"; cin.getline(string, 100); len = strlen(string); for(i=0; string[i] != '\0'; i++) { if(string[i] == ' ') { string[i] = '-'; } } cout<<"\nHere is the string after replacing space with hyphen\n"; for(i=0; i<len; i++) { cout<<string[i]; } getch(); }
The output of the above C++ string program is shown here:

Following is another C++ string example:
/* C++ String - Example Program */ #include<iostream.h> #include<stdio.h> #include<conio.h> void main() { clrscr(); char string[100]; int count=0, i; cout<<"Enter a sentence\n"; gets(string); for(i=0; string[i] != '\0'; i++) { switch(string[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': count++; } } cout<<"\nThe above sentence contains total "<<count<<" number of vowels."; getch(); }
The above C++ string program will produce the following output:

Here is another C++ string example:
/* C++ String - Example Program */ #include<iostream.h> #include<string.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char string[100], word[20]; int i, m=0, n; cout<<"Enter a sentence\n"; gets(string); strcat(string, " "); for(i=0; string[i] != '\0'; i++) { if(string[i] != ' ') { word[m] = string[i]; m++; } else { while(m>0) { cout<<word[--m]; } cout<<string[i]; } } getch(); }
Here is the sample run of the above C++ String example:

Let's take another C++ string example:
/* C++ String - Example Program */ #include<iostream.h> #include<conio.h> #include<ctype.h> #include<stdio.h> void main() { clrscr(); char string[100]; int i; cout<<"Enter a sentence\n"; gets(string); string[0] = toupper(string[0]); for(i=0; string[i] != '\0'; i++) { if(string[i] == ' ') { string[i+1] = toupper(string[i+1]); } } cout<<"\nHere is the string after capitalizing every first letter of the word\n"; cout<<string; getch(); }
Here is the output of the above C++ string example:

Following is one more example on C++ string:
/* C++ String - Example Program */ #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char string[100], ch; int i, j, l=0, m, len; cout<<"Enter a sentence\n"; gets(string); len = strlen(string); for(i=0; i<len; i++) { j=i; while(string[j] != ' ' && string[j] != '\0') { j++; } l = j; if(j%2==0) { l = j; } else { l = j-1; } for(m=i; m<l; m=m+2) { ch = string[m]; string[m] = string[m+1]; string[m+1] = ch; } i = j+1; } cout<<string; getch(); }
Here is the sample run of this C++ string example:

Here is the last program on C++ string helps you to understand string in C++ completely:
/* C++ String - Example Program */ #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr(); char string[100], word[20]; int i, j, k, lenm, spos, lpos, lens; cout<<"Enter a sentence\n"; gets(string); cout<<"Enter a word to find\n"; gets(word); lenm=strlen(string); for(i=0; i<lenm; i++) { j = i; if(string[j] == word[0]) { k = 0; while(string[j++] == word[k++]); if(word[k-1]=='\0') { spos=i; break; } else { i=j-1; } } else { i++; } } lens=strlen(word); for(i=lenm-1; i>=0;) { j=i; if(string[j]==word[lens-1]) { k=lens-1; while(string[j--]==word[k--]); if(k<0) { lpos=j+2; break; } else { i=j+1; } } else { i--; } } cout<<"First occurrence of "<<word<<" is at position "<<spos+1<<"\n"; cout<<"Last occurrence of "<<word<<" is at position "<<lpos+1; getch(); }
Following is the sample run produced by the above C++ string example program:

« Previous Tutorial Next Tutorial »