String in C++

Advertisements

Prev Tutorial Next Tutorial

String in C++

String is a collection of character or group of character, it is achieve in C language by using array character. The string in C language is one-dimensional array of character which is terminated by a null character '\0'. In other words string is a collection of character which is enclose between double cotes ( " " ).

Note: Strings are always enclosed within double quotes. Whereas, character is enclosed within single quotes in C.

Declaration of string

Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.

String in C++

Important points for Declaration of string

  • In declaration of string size must be required to mention otherwise it gives an error.

Syntax

char str[];   // Invalid
char str[10]; // Valid
  • In declaration of the string size must be unsigned integer value (not -ve or zero value) which is greater than zero only.

Example

	
char str[];   // Invalid
char str[0];  // Invalid
char str[-1]; // Invalid
char str[10]; // Valid

Syntax

	
char variable_name[SIZE];
	
char str[5];

Initializing Array string

According to array initialization concept

String in C++

Example

	
char str[]="abcd";
        OR
char str[5]="abcd";
        OR
char str[5]={'a','b','c','d','\0'};
        OR
char str[]={'a','b','c','d','\0'};
        OR
char str[5]={'a','b','c','d','\0'};

Important points for Initialization of the string

  • In Initialization of the string if the specific number of character are not initialized it then rest of all character will be initialized with NULL.

Syntax

	
char str[5]={'5','+','A'};
    str[0];  ---> 5
    str[1];  ---> +
    str[2];  ---> A
    str[3];  ---> NULL
    str[4];  ---> NULL
  • In initialization of the string we can not initialized more than size of string elements.

Syntax

	
char str[2]={'5','+','A','B'};  // Invalid
  • In initialization of the string the size is optional in this case how many variable elements are initialized it, that array element will created.

Syntax

	
char str[]={'5','+','A','B'};  // Valid
sizeof(str)  --> 4byte

When we are working with character array explicitly NULL character does not occupies any physical memory at the end of the character array.

Syntax

	
char str[]={'h','e','l','l','o'};
sizeof(str)  --> 5byte

String data at the end of the string NULL character occupies physical memory.

Syntax

	
char str[]="hello";
sizeof(str)  --> 6byte

Reading String from user

Syntax

	
char str[5];
cin>>str;

Note: String variable str can only take only one word. It is because when white space is encountered, the scanf() function terminates.

Syntax

	
char str[5];
gets(str);

gets()

gets() are used to get input as a string from keyword, using gets() we can input more than one word at a time.

Example of String program in c language

Example

	
#include<iostream.h>
#include<conio.h>

void main()
{
char str[10];
cout<<"Enter any string: ";
gets(str);
cout<<"String are: ";
puts(str);
getch();
}

Explanation: Here gets() function are used for input string and puts() function are used to show string on console or monitor.

Output

Enter String: hello word
String are: hello word

C Library String functions

All the library function of String is available in String.h header file.

S.N.FunctionPurpose
1strcpy(s1, s2)Copies string s2 into string s1.
2strcat(s1, s2)Concatenates string s2 onto the end of string s1.
3strlen(s1)Returns the length of string s1.
4strcmp(s1, s2)Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5strchr(s1, ch)Returns a pointer to the first occurrence of character ch in string s1.
6strstr(s1, s2)Returns a pointer to the first occurrence of string s2 in string s1.

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements