[go: up one dir, main page]

0% found this document useful (0 votes)
28 views12 pages

Lecture 2.2.4 Strings

1. A string in C is an array of characters ended by a null character. Strings are enclosed in double quotes, while characters are in single quotes. 2. Strings can be initialized and memory is allocated based on initialization. For a fixed length array, memory for the specified length is set aside. 3. Functions like gets and puts can be used to input and output strings. scanf encounters whitespace which gets ignores.

Uploaded by

siyalohia8
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)
28 views12 pages

Lecture 2.2.4 Strings

1. A string in C is an array of characters ended by a null character. Strings are enclosed in double quotes, while characters are in single quotes. 2. Strings can be initialized and memory is allocated based on initialization. For a fixed length array, memory for the specified length is set aside. 3. Functions like gets and puts can be used to input and output strings. scanf encounters whitespace which gets ignores.

Uploaded by

siyalohia8
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/ 12

• String Definition

• Initialization of string
Content • Memory Representation of String
• String I/O functions
• Examples
• References

1
Def: A string in C is simply an array of characters ended with null character (‘\
0’) This null character indicates the end of the string.
Strings are always enclosed by double quotes. Whereas, character is enclosed
by single quotes in C.

INITIALIZATION OF STRING:

String char string[10] = {‘h’, ’e’, ‘l’, ‘l’, ‘o’, ‘\0’};


(or)
char string[10] = “Hello”;
(or)
char string [] = “Hello”;

Difference between above declarations are, when we declare char as


“string[10]”, 10 bytes of memory space is allocated for holding the string
value. When we declare char as “string[]”, memory space will be allocated as
per the requirement during execution of the program.
2
Let’s take an example for Memory representation of string:
char string[10]=”Hello”

Memory
Representation
in Strings
If you do not place null character at the end of string, the C compiler
automatically places the '\0' at the end of the string when it initializes
the array. If we print above mentioned string in C:
#include <stdio.h>
int main ()
{
char string[10] = {'H', 'e', 'l', 'l', 'o', '\
0'};
printf("Message is: %s\n", string);
return 0; 3
Input function scanf() can be used with %s format specifier to read a string
input from the terminal. But there is one problem with scanf() function, it
terminates its input on the first white space it encounters. Therefore if you
try to read an input string "Hello World“ using scanf() function, it will only
read Hello and terminate after encountering white spaces.

String Input and


Output function

4
You can use the gets() function to read a line of string. And, you can
use puts() to display the string.
Example1 : gets() and puts()
#include <stdio.h>
int main()
How to read a {
line of text? char name[30];
printf("Enter name: ");
gets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}

5
Example2: Concatenate Two Strings
#include <stdio.h>
int main()
{
char s1[100] = "programming ", s2[] = "is awesome";
int length, j; // store length of s1 in the length variable
length = 0;
while (s1[length] != '\0')
Example of string {
++length;
} // concatenate s2 to s1

for (j = 0; s2[j] != '\0'; ++j, ++length)


{
s1[length] = s2[j];
} // terminating the s1 string
Here, two strings s1 and s2 and
concatenated and the result is
s1[length] = '\0';
stored in s1.
printf("After concatenation: ");
It's important to note that the
puts(s1);
length of s1 should be sufficient
return 0;
to hold the string after
}
concatenation.
1.
A string in C is simply an
array of characters ended
with null character (‘\0’) This
null character indicates the
end of the string.

SUMMARY 2. 3.
Strings are always
enclosed by double Use gets and puts
quotes. Whereas, functions for input a
character is enclosed string
by single quotes in
C.
PROGRAMS

Q1 Write a C program to count no of lines, words and


characters in a given text.
Q2 C program to print all VOWEL and CONSONANT characters
separately
FREQUENTLY Q3 C program to count upper case, lower case and special
characters in a string.
ASKED Q4 C program to print following pattern.
QUESTIONS H
He
Hel
Hell
Hello

8
1 What is the Format specifier used to print a String or Character array in C
Printf or Scanf function.?
A) %c
B) %C
C) %s
UTILISE D) %w
YOUR
2. What is the output of C Program with Strings.?
KNOWLEDGE int main()
TO ANSWER {
Let us see how much you have char ary[]="Discovery Channel";
learned from the lecture and printf("%s",ary);
how effectively you can apply
return 0;
your knowledge…!!
}
A) D
B) Discovery Channel
C) Discovery
D) Compiler error 9
3 What is the output of C Program with Strings.?
{
char str[]={'g','l','o','b','e'};
printf("%s",str);
return 0;
}
UTILISE A) g
B) globe
YOUR C) globe\0
KNOWLEDGE D) None of the above
4 What is the output of C Program.?
TO ANSWER int main()
Let us see how much you have { int str[]={'g','l','o','b','y'};
learned from the lecture and printf("A%c ",str);
how effectively you can apply
printf("A%s ",str);
your knowledge…!!
printf("A%c ",str[0]);
return 0; }
A) A A A
B) A Ag Ag
C) A*randomchar* Ag Ag 10
Book References:
[1] Thareja Reema (2014) Programming in C. 2 nd ed.
[2] Zed A. Shaw, Learn C the Hard Way’
[3] https://en.wikibooks.org/wiki/C_Programming

REFERENCES
Vedio Lecture:
BOOKS https://spocathon.page/video/lecture-32-character-array-and-strings

WEBSITES https://www.youtube.com/watch?v=_3CmPbInJJs

COURSES
Websites:
https://www.tutorialspoint.com/objective_c/objective_c_strings.htm
https://www.programiz.com/c-programming/c-strings
https://www.studytonight.com/c/c-input-output-function.php

11
THANK YOU

12

You might also like