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

Lecture PDF On Strings C

The document provides an overview of strings in C including definition, initialization, memory representation, input/output functions and examples. It defines a string as an array of characters ended by a null character. Functions like scanf(), gets() and puts() are described for string input/output. Examples on concatenation and pattern printing are also given.

Uploaded by

crmbd965jh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lecture PDF On Strings C

The document provides an overview of strings in C including definition, initialization, memory representation, input/output functions and examples. It defines a string as an array of characters ended by a null character. Functions like scanf(), gets() and puts() are described for string input/output. Examples on concatenation and pattern printing are also given.

Uploaded by

crmbd965jh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Introduction to Problem Solving
Code:23CSH-101

Strings DISCOVER . LEARN . EMPOWER


1
OBJECTIVES
The course aims to provide exposure to problem solving with programming

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

CO1 Remember the concepts related to fundamentals of C language,


draw flowcharts and write algorithm/pseudocode.
CO2 Understand the way of execution and debug programs in C
language.
CO3 Apply various constructs, loops, functions to solve mathematical
and scientific problem.
CO4 Analyze the dynamic behavior of memory by the use of pointers.

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:

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.
6
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; 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.

String Input and


Output function

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

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

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

You might also like