You are on page 1of 32

Module – 2

C Strings and its Operations

Dr.Jayanthi R
SCOPE
String Definition

• In real time, we use a lot of applications which processes words and sentences to find pattern in 2
user data, replace it , delete it , modify it etc.
String Definition
• A string is a sequence of characters which is treated as a single
data item in C.
• It can also be said as an array of characters and any group of
characters defined between double quotations is string constants.
• Example of String:
“trytoprogram”
• try2program is a string with a group of 11
characters.
• Each character occupies 1 byte of memory.
• These characters are placed in consecutive memory locations;
• string is an array of characters.

3
String Definition

4
String Definition

5
String and memory
• String is the sequence of arrays and is placed in consecutive
memory locations.
• To indicate the termination of string a null character is always
placed at the end of the string in the memory.
The above string will be stored in memory location like this:

Example:2
char string[] = {"Hello"};

6
Declaring and Initializing string variables
• String is not a data type in C, so character arrays are used to
represent strings in C and are declared as:
char string_name [size];
• The length of a string is always greater than the number of string
characters by one because when a compiler assigns a character
string to a character array, it automatically supplies a null
character (‘\o’) at the end of the string.
• A string constant is a one-dimensional array of characters
terminated by a null ( ‘\0’ ).
Declaration of Character:
char mychar;
Declaration of String:
char myString[10];
• The characters after the null character are ignored. 7
Initializing string arrays:
Strings in C can be initialized in following ways:
• char string_name [12] = "try2program";
• char string_name [12] = {'t','r','y','2','p','r','o','g','r','a','m','\0'};
• In the above example, the size of an array will be determined
automatically by the compiler.

8
Initialization String -Compilation time initialization
Initialization Syntax:

• When declaring a string don’t forget to leave a space for the null
character which is also known as the string terminator character
Why null Character?
This is the only way the functions that work with a string can know
where the string ends.

char myString[100] = “Initial value”

9
Initialization String
Note: Difference between 0, ‘0’, ‘\0’, “0”.

10
Initialization String (Run time Initialization )

11
Common Error

12
How to read strings from users?
• There are two ways for reading the string from users:
1. using scanf() function
2. using getchar() and gets() functions
using scanf function
• The method of reading string using input function scanf() with %s
format specifications is the most famous method.
• with this scanf() function, we can only read a word because this
function terminates its input on the first white space it encounters.
For example:

Note: Normally, an ampersand is used before scanf() function while


reading the value of variables but in the case of character arrays, we
don’t need an ampersand (&) because string name itself acts as 13a
using getchar() and gets() functions
• scanf function is used to read only a word and to read a whole line
of text either getchar() or gets()function is used.
• This function can be used to read successive single characters
from the input until a new line character '\n' is encountered and
then null character is inserted at the end of the string.
Syntax of getchar() function

Note: getchar() function has no parameters.


Syntax of gets() function

14
using getchar() and gets() functions
#include <stdio.h>
int main () {
char c;
printf("Enter character: ");
c = getchar();
printf("Character entered: ");
putchar(c);
return(0);
}

15
using getchar() and Putchar() functions
• putchar() function is a file handling function in C programming
language which is used to write a character on standard
output/screen.
• getchar() function is used to get/read a character from keyboard
input.
• Please find below the description and syntax for above file handling
function.

16
using getchar() and Putchar() functions

17
using gets() & puts() functions
• The gets() functions are used to read string input from the
keyboard and puts() function displays it.
• These functions are declared in the stdio.h header file.
gets() function
• The gets() function is similar to scanf() function but it allows
entering some characters by the user in string format with the
double quotation. and stored in a character array format.

18
using gets() & puts() functions
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch[30];
printf("Enter the string: ");
gets(ch); /reading string using gets()
printf("you entered string here\n");
printf(ch);//display out put on the screen using printf() function
return 0;
}

19
using gets() & puts() functions
Puts() function
• The puts() function is similar to printf() function.
• But puts() function is used to display only the string after reading by
gets() function entered by user(gets similar to scanf() function)

• O/P

20
String Manipulations in C Programming Using Standard Library Function
• All these string handling functions are defined in the header file string.h.
• So every time we use these string handling functions string.h header file must be
included.
• Some of the major string handling functions used are tabulated below.

21
String Manipulations in C Programming Using Standard Library Function
String Copy (strcpy)

22
String Manipulations in C Programming Using Standard Library Function
String Copy (strcpy())

#include<stdio.h>
#include<string.h>

main()
{
char source[] = "C Program";
char destination[50];
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
return 0;
}

23
String Manipulations in C Programming Using Standard Library Function
String Compare (strcmp())

24
String Manipulations in C Programming Using Standard Library Function
String Compare (strcmp())

25
String Manipulations in C Programming Using Standard Library Function
String Compare (strcmp())

26
String Manipulations in C Programming Using Standard Library Function
String Compare (strcmp)

27
String Manipulations in C Programming Using Standard Library Function
String Length (strlen())

28
String Manipulations in C Programming Using Standard Library Function
String Length (strlen())

29
Two dimensional- string array

30
Two dimensional- string array

31
Summary
Reading Strings
•Using scanf() function
•Using gets() function
•Using fgets() function
•Using getline() function
•Using getdelim() function
•Using getchar() function

Printing Strings
•Using printf() function
•Using puts() function
•Using putchar() function

32

You might also like