codescracker


c++

C++ String



« Previous Tutorial Next Tutorial »


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:

c++ strings

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.

strings in c++

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:

c++ string

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:

string in c++

c++ string example

c++ string program

string example in c++

string program in c++

More Examples

Here are the list of some more examples on C++ strings:

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:

c++ string tutorial

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:

c++ string with examples

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:

string in c++ example

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:

string example c++

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:

string c++

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:

strings c++

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:

c++ complete string tutorial

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test