Sei sulla pagina 1di 17

String in C

Goutam Majumder

Lovely Professional University


goutam.23320@lpu.co.in

October 22, 2018

Goutam Majumder (LPU) Strings in C October 22, 2018 1 / 17


Overview

1 Introduction
2 Input a String
3 Issues Related to scanf()
4 puts() vs printf()
5 String Library Functions
6 strlen() vs sizeof() in C
7 Palindrome Problem
8 Some String Programs
9 Extra Out of Box

Goutam Majumder (LPU) Strings in C October 22, 2018 2 / 17


Basic Introduction of String in C
What is a String?
Strings are defined as an array of characters. The difference between a char-
acter array and a string is the string is terminated with a special character
“\0”.

Declaration a String
Declaring a string is as simple as declaring a one dimensional (1-d) character
array. Below is the basic syntax for declaring a string.

char str name[size];

In the above syntax str name is any name given to the string variable and
size is used define the length of the string, i.e the number of characters
strings will store. Please keep in mind that there is an extra terminating
character which is the null character (‘\0’) used to indicate termination of
string.
Goutam Majumder (LPU) Strings in C October 22, 2018 3 / 17
Introduction to String in C

Initializing a String
A string can be initialized in different ways. We will explain this with the
help of an example. Below is an example to declare a string with name as
str and initialize it with “Lovely”.

char str[ ] = “Lovely”; char str[10] = “Lovely”;

char str[ ] = {‘L’,‘o’,‘v’,‘e’,‘l’,‘y’,‘\0’};

char str[10] = {‘L’,‘o’,‘v’,‘e’,‘l’,‘y’,‘\0’};

Goutam Majumder (LPU) Strings in C October 22, 2018 4 / 17


String in C
Initialization and Print a String

We can see in the above program that strings can be printed using a normal
printf statements just like we print any other variable. Unlike arrays we do
not need to print a string, character by character. The C language does not
provide an inbuilt data type for strings but it has an access specifier “%s”
which can be used to directly print and read strings.

Goutam Majumder (LPU) Strings in C October 22, 2018 5 / 17


Input a String
As a character Array

Goutam Majumder (LPU) Strings in C October 22, 2018 6 / 17


Input a String
Using Library Function gets() and fgets()

Input = “Lovely Professional” Output = Lovely Professional

Input = “Lovely Professional” Output = Lovel using fgets()

Goutam Majumder (LPU) Strings in C October 22, 2018 7 / 17


Issues Related to scanf() with String

It doesn’t required an ampersand


– scanf() needs the address of the variable to read into, and string buffers
are already represented as addresses (pointer to a location in memory, or an
array that decomposes into a pointer).
– printf does the same, treating %s as a pointer–to–string.

scanf(“%s”,str);

How take multi–word string as input?


Here, [ ] is the scanset character. ˆ\n tells to take input until newline
doesn’t get encountered. Then, with this %*c, it reads newline character
and here used * indicates that this newline character is discarded.

scanf(“%[ˆ\n]%*c”,str);

Goutam Majumder (LPU) Strings in C October 22, 2018 8 / 17


puts() vs printf()

puts do not require any format specifier like %d or %f or %c but printf


requires format specifiers according to type of the output.
In printf(),we have to use ‘\n’ to go to next line but in puts function,
we do not require ‘\n’ ,rather we automatically go to next line.
required header file is stdio.h

puts(str); printf(“%s”,str);

If you do not want the cursor to be moved to next line, then you can
use following variation of puts().

fputs(str,stdout);

Goutam Majumder (LPU) Strings in C October 22, 2018 9 / 17


String.h Header File

Goutam Majumder (LPU) Strings in C October 22, 2018 10 / 17


strlen() vs sizeof() in C

sizeof()
sizeof operator is a compile time unary operator which can be used to com-
pute the size of its operand.
– The result of sizeof is of unsigned integral type which is usually denoted
by size t.
– sizeof can be applied to any data-type, including primitive types such as
integer and float, or compound datatypes such as structure, union etc.

strlen()
strlen() is a predefined function whose definition is contained in “string.h”.
– strlen() accepts a pointer to an array as argument and walks through
memory from the address and looking for a NULL character and counts up
how many memory locations it passed before it finds one.
– The main task of strlen() is to count the length of an array or string.

Goutam Majumder (LPU) Strings in C October 22, 2018 11 / 17


strlen() vs sizeof()

Type: Sizeof operator is a unary operator whereas strlen() is a prede-


fined function in C
Data types supported: sizeof gives actual size of any type of data
(allocated) in bytes (including the null values) whereas get the length
of an array of chars/string.
Evaluation size: sizeof() is a compile-time expression giving you the
size of a type or a variable’s type. It doesn’t care about the value of
the variable.
Strlen on the other hand, gives you the length of a C–style NULL–
terminated string.

Goutam Majumder (LPU) Strings in C October 22, 2018 12 / 17


Palindrome Problem

Goutam Majumder (LPU) Strings in C October 22, 2018 13 / 17


Find the Length of a given string

Input = “Lovely Professional University” Length of the String: 30

Goutam Majumder (LPU) Strings in C October 22, 2018 14 / 17


Reverse of a String

Input = “Lovely” Output = ylevoL

Goutam Majumder (LPU) Strings in C October 22, 2018 15 / 17


Extra Study Materials on String in C

Storage for String in C


Different methods to reverse a string in C
gets() is risky to use!
Memory layout of a C Program
Difference between character array and character pointer
String Manipulatio: Using library functions

Move your cursor to individual item and click

Goutam Majumder (LPU) Strings in C October 22, 2018 16 / 17


The End

Goutam Majumder (LPU) Strings in C October 22, 2018 17 / 17

Potrebbero piacerti anche