Discuss in detail about strings

Estudies4you
Q28. Discuss in detail about strings.
Answer:

String
In C++, string is defined as a set of characters stored in memory in sequential order. Strings are enclosed in a single or double quotes and end with a null character ‘\0’.
Syntax
char<stringname>
Here, char is the data type and string name is the name of the string. A string is stored in the memory in the below given manner.
H
e
l
l
o
\0
Here, the compiler stores five characters of string “Hello” in five entries and the null character in last entry.

Classification of String
Strings are of two types. They are,
1. Fixed length strings
2. Variable length strings.

l. Fixed Length Strings
Fixed-length strings have a fixed length which is set at the time of their creation.
The first decision in implementing fixed-length strings is size of the variable. If the size of the variable is small then, it cam not store all the data. On the other hand, if the size of the variable is larger, then memory is wasted.
In addition, it is difficult to identify the data from a set of non-data. However, this problem can be overcomed by adding non-data characters like spaces-at the end of the data.

2 Variable Length Strings
Variable-length strings vary in size. They have no maximum limit. Inorder to identify the end of a variable- length strings two techniques are used. They are,
(i) Length controlled strings
(ii) Delimited strings.

(i) Length Controlled Strings
Length controlled strings are the strings that add a count at the beginning of the string. This count represents the number - of characters present in the string. String manipulation functions use this count to find the length of the string. Figure (1) illustrates the format of length-controlled string.
Discuss in detail about strings in c++,define string in c++,syntax for string in c++,string syntax in c++,Classification of String in c++,Fixed Length Strings in c++,Variable Length Strings in c++,Length Controlled Strings in c++,Delimited Strings in c++,example programs of strings in c++,jntuh c++ lecture notes,jntuh c++ notes,jntu c++ lecture notes,c++ study material jntu,estudies4you,
Fig (1). Length controlled string format
(ii) Delimited Strings:
Delimited strings use a delimiter at the end of the string. The null character (\0), which is the very first character in the ASCII character set is used as a delimiter. This delimiter (\0) cannot be used as data. Figure (2) below illustrates the format of delimited string.
Discuss in detail about strings in c++,define string in c++,syntax for string in c++,string syntax in c++,Classification of String in c++,Fixed Length Strings in c++,Variable Length Strings in c++,Length Controlled Strings in c++,Delimited Strings in c++,example programs of strings in c++,jntuh c++ lecture notes,jntuh c++ notes,jntu c++ lecture notes,c++ study material jntu,estudies4you,
Fig (2). Delimited string format
Many operations can be performed using strings such as,
  • Reading strings
  • Writing strings
  • Comparing strings
  • Copying one string to another
  • Extracting (fetching) a portion of string
  • Concatenating two strings.
Program
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
int count1=0, count2=0;
char str1[8] ={‘w’,‘e’,‘l’,‘c’,‘o’,‘m’,‘e’};
cout << strl<< endl;
char str2[]={‘h’,‘e’,‘l’,‘l’,‘o’};
cout << str2 << endl;
for(int i=0; str1 [i]; i++)
count1++;
cout << "Number of characters in strl is "<< count1;
for(int i=0; str2[i]; i++)
count2++;
cout << "\nNumber of characters in str2 is "<< count2;
return 0;
}


Output
Discuss in detail about strings in c++,define string in c++,syntax for string in c++,string syntax in c++,Classification of String in c++,Fixed Length Strings in c++,Variable Length Strings in c++,Length Controlled Strings in c++,Delimited Strings in c++,example programs of strings in c++,jntuh c++ lecture notes,jntuh c++ notes,jntu c++ lecture notes,c++ study material jntu,estudies4you,

To Top