0% found this document useful (0 votes)
8 views

Program For Problem Solving Unit3

Uploaded by

nyxx4811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Program For Problem Solving Unit3

Uploaded by

nyxx4811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit 3 – String Basics

In C programming, a string is a sequence of characters terminated with a null character \


0.
For example: char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double
quotation marks, it appends a null character \0 at the end by default.
In C language strings are stored in array of char type along with null terminating
character \0.
Synatx:
char fname[4];
The above statement declares a string called fname that can take up to 3 characters.
fname[]={‘J’,’O’,’E’}
J O E \0
char name[10]; can store maximum of 9 characters.
Unit 3 – String Declaration and Initialization
• How to declare a string?
• char s[5];

• How to initialize strings?


• char s[] = "abcd";
• char s[50] = "abcd";
• char s[] = {'a', 'b', 'c', 'd', '\0'};
• char s[5] = {'a', 'b', 'c', 'd', '\0'};
Unit 3 – String Functions

• 1. gets()
• 2. puts()
• 3. getchar()
• 4. putchar()
• 5. Printf()
Unit 3 – String Functions
• 1. gets()
• It is used to take a single input at a time but can be used to input a
complete sentence with spaces unlike scanf(). It stops reading character
when the newline character is read or end-of the file is reached.
• Declaration
• char *gets(char *str)
• str- This is the pointer to an array of characters where the C string is
stored.
• Return Value:
• This function returns str on success, and NULL on error or when end of
file occurs.
Unit 3 – String Functions
Unit 3 – String Functions
• 2. puts()
• This function writes strings or lines to stdout, i.e, the output stream. The string is
printed with newline and an integer value is returned.
• Declaration
• int puts(const char* string)
• Example:
# include<stdio.h>
void main(){
// Initializing the string.
char string[] = "SRM University";
// Writing our string to stdout.
puts(string);
}
Unit 3 – String Functions
• OUTPUT

You might also like