String Handling Programming Exercise 2 in C/C++

What is String in C/C++?

String can be defined as the combination of characters. String handling is used to manipulate strings such as finding number of characters in a file or comparing two files. String are commonly used with file handling.

File is a sequence of bytes. Input and output streams are used to store and retrieve strings in file.

Syntax of String

Data_type var_name[length];

For example

char name[40];

In the above statement, we declare a variable name with a length of 40 characters.

This variable occupies 40 bytes in memory.

One character occupies one byte in memory.

In other words, if we want to store a single character in computer we require one byte.

Exercise Question 2

Write a program that inputs your name and displays number of words and number of characters without spaces in the your name.

using namespace std;

	char name[100];
	int word, ch, i;
	word = ch = i = 0;
	cout<<"Enter Your Name:";
	cin.getline(name, 100);
	while(name[i]!='\0')//\0 means End of File Character.
		if(name[i] ==' ')
	cout<<"Number of words in your name : "<<word+1<<endl;
	cout<<"Number of characters in your name: "<<ch<<endl;

Following Functions are commonly used in String Handling

getline() Function

getline function is used to input a string from user. following is the syntax of this function


strcpy() Function

strcpy() function is used to copy a string. Following is the syntax


strrev() Function

This function is used to reverse a string. 



strcmp() Function

This Function is used to compare two strings.