Lecture PDF On Strings C
Lecture PDF On Strings C
OBJECTIVES The course aims to raise the programming skills of students via logic building
capability
With the knowledge of C language students would be able to model real world
problems
2
CO Course Outcome
Number
CO5 Design and develop modular programs for real world problems
using control structure and selection structure.
3
Scheme of Evaluation
4
• String Definition
• Initialization of string
Content • Memory Representation of String
• String I/O functions
• Examples
• References
5
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:
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; 7
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.
8
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;
}
9
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
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
12
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 13
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 14
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
15
THANK YOU
16