Strings

Hello,

What do you know about Strings?

Where are they used?

Differences in strings and character arrays?

How often will you need it?

Strings (Character Arrays)

STRING: It is an array of type char.

Syntax for declaration

 

char <array/string name> [max. number of characters to be stored +1];

 

The number of elements that can be stored in a string is always n-1, if the size of the array specified is n. This is because 1 byte is reserved for the NULL character '\0' i.e. backslash zero. A string is always terminated with the NULL character.

Example:
char str[80];
In the above example, str can be used to store a string with 79 characters.

Initializing a string

 

A string can be initialized to a constant value when it is declared.

 

char str[ ] = "Good";
    Or
char str[]={'G','o','o','d','\0'};

 

Here. 'G' will be stored in str[0], 'o' in str[1] and so on.

Reading strings with/without blanks

 

To read a string without blanks cin can be used
cin>>str;


To read a string with blanks cin.getline() or gets() can be used.

cin.getline(str,80);
   -Or-
gets(str);

Printing strings

cout and puts() can be used to print a string.


cout<<str:
   Or
puts(str);

C++ and C

Are they same?

If no then why?

C

C++

C was developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs.

C++ was developed by Bjarne Stroustrup in 1979 with C++'s predecessor "C with Classes".

When compared to C++, C is a subset of C++.

C++ is a superset of C. C++ can run most of C code while C cannot run C++ code.

For now you need only this to work.

C

#include <stdio.h>

int main() {
     printf("Hello World");
     return 0;
}
#include <stdio.h>
int main() {
     printf("Hello World");
     return 0;
} 

C++

Write a program for ...?

Try out the things and you will get it ;)

 

Questions?

deck

By Vijay Krishnavanshi

deck

  • 845