Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, March 3, 2020

String in C

References:

String in C:

string in C is an array of characters OR string is a sequence of characters stored in a character array terminated by a special character null (‘\0’) which is used to manipulate text such as word or sentences. The length of a string is determined by a terminating null character: '\0’. So, a string with the contents, say, "abc" has four characters: 'a' , 'b' , 'c' , and the terminating null ( '\0' ) character. The terminating null character has the value zero. Each character in the array occupies one byte of memory, and the last character must always be 0.

string enclosed in double quotation marks. A character such as ’d’ is not a string and it is indicated by single quotation marks. 'C' provides standard library functions to manipulate strings in a program. For example, the word "Protec" and the phrase "Lets Join Protec" are both strings. Even "12345" could be considered a string.

I will update related exmple soon..

Declaration of String:

Declaring a string is as simple as declaring a one dimensional array.


Syntaxchar string_name [size_of_String]; 
               
Example: char str[7];

Initialization of string:

A more convenient way to initialize a C string is to initialize it through character array: char str[ ] = "Join Protec"; This is same as initializing it as follows: char str[ ] = { 'J', 'o', 'i', 'n', ' ', 'P', 'r', 'o', 't', 'e', 'c', '\0' }; It can be initialized by two ways:

  • By Char Array
  • By String Literal

Declaring by char Array:  As we know that each character enclosed in single quotation marks separated by a comma and string enclosed in double quotation marks. While declaring string size of an array is not mandatory to define in square bracket, so we can be define by either way as below:



Example 1:



·    Char str [7] = {‘P’, ‘R’, ‘O’, ‘T’, ‘E’, ‘C’, ‘\0’};

OR

·    Char str [ ] = {‘P’, ‘R’, ‘O’, ‘T’, ‘E’, ‘C’, ‘\0’};



Example 2:       


Char str [17] = {‘L’, ‘e’, ‘t’, ‘s’, ‘ ’, ‘J’, ‘o’, ‘I’, ‘n’, ‘ ’, ‘P’, ‘r’, ‘o’, ‘t’, ‘e’, ‘c’, ‘\0’};

                                        OR
Char str [ ] = {‘L’, ‘e’, ‘t’, ‘s’, ‘ ’, ‘J’, ‘o’, ‘I’, ‘n’, ‘ ’, ‘P’, ‘r’, ‘o’, ‘t’, ‘e’, ‘c’, ‘\0’};


Declaring by String Literal:  In this type (in String Literal) of declaration NULL character (‘\0’) will automatically be inserted at the end of the string.

Example 1:

·    Char str [6] = “PROTEC”;
                  OR
·    Char str [ ] = “PROTEC”; 

Example 2: 

·    Char str [17] = “Lets Join PROTEC”
                   OR
·    Char str [ ] = “Lets Join PROTEC”; 

The length of a string is determined by a terminating null character: '\0’. So, a string with the contents, say, "abc" has four characters: 'a' , 'b' , 'c' , and the terminating null ( '\0' ) character.

For example, the word "Protec" and the phrase "Lets Join Protec" both are strings.

Memory Representation of String:

In the below example, in the sentence “Lets Join Protec” there are two spaces and one null character which also considered as character. That's how the total length would be 17 characters. And in word “PROTEC” has 7 characters.

I will update related example soon...

String I/O Function in C:

Input functions: scanf () and gets () both are used to reads inputs given by user. Difference between them is: scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it encounters whitespace instead it takes whitespace as a string.

I will update related example soon..

Output functions: Printf () and puts () both are used to display outputs on the screen. Difference between them is: the printf () function is used to print both strings and variables to the screen while the puts () function only permits you to print a string only to your screen.

I will update related example soon..

Note:The scanf() and printf() are generic i/o functions that they support all built-in data types such as int, char, float, long, double, hexadecimal values as well as strings.

Read & Write String in C using Printf() & Scanf() function:       


As you know that scanf() is used for reading inputs given by the user but it has a has a drawback which stops reading input when it encounters whitespace. Let’s consider a scenario in the below example.

Note: format specifier %s is used for string input/output.


I will update related example soon..



It is clear in the above output that scanf () does not allow to read the space separated strings.


To resolve this issue, instead of writing Scanf(“%s”, str); either we must write like this Scanf(“%[^\n]s”,  str);  which instruct the compiler to store space separated string. This way you can allow scanf() to read string even when it encounters white spaces. OR can be use gets() function which is also use to read the space-separated strings. Let’s consider a another scenario in the below example: 

I will update related example soon..



Note: Here you can notice that there is no need to use address of (&) operator in scanf() to store a string since String str is an array of characters and  by writing the name of array i.e. str, indicates the base address of the array. Therefore we need not use address of (&) operator in.   

Read & Write String in C using gets() & puts() function: 


I will update related example soon..

Traversing String with Example:


C program to calculate length of the string without using Null (‘\0’):
  • Read a string.
  • Create a function and pass string within it.
  • Take a counter as length (that will store the length of the string), initialize with 0.
  • Run a loop until NULL character is not found.
  • Increase the counter (length)
  • As NULL founds return the length.
I will update related example soon...

Use of gets:


The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings.

fputs() fgets() function:


The fputs() function is used to write string(array of characters) to the file. fputs(char str[], FILE *fp); The fgets() function is used to read string(array of characters) from the file.

String Manipulations:

Strings handling functions are defined under "string.h" header file

strncat(): ..............................................
strcat(): Concatenates(joins) two strings.
strcpy(): Copies a string to another.
strcmp(): Compares two strings.
strlwr():  Converts string to lowercase.
strupr(): Converts string to uppercase.
strlen(): 
Finds out the length of the string.
strlwr(): It converts string to lowercase.

String variables:

String variables are variables that hold zero or more characters such as letters, numbers, spaces, commas and many more. You can't use numeric functions such as addition or subtraction on string variables.

      











Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us