1 of 20

Dr. S. Sridevy

Associate Professor (CS)

Dr. M. Ramanan

Teaching Assistant (CS)

Dept. of PS&IT, AEC&RI, TNAU, Coimbatore -3.

String: Reading and Writing Strings - String handling functions

COM 102 Theory Schedule 10

2 of 20

Concept of String

  • A string is an array of characters.

  • Any group of characters (except the double quote sign) defined between double quotation marks is a constant string.

S. Sridevy

7/4/2022

3 of 20

Concept of String

Eg: 1) “Man is obviously made to think”

If we want to include a double quote in a string, then we may use it with the back slash.

Eg: printf(“\”well done!\””);

will output

“well done!”

S. Sridevy

7/4/2022

4 of 20

Character String - Operations

  • Reading and writing strings.
  • Combining strings together.
  • Copying one string to another.
  • Comparing strings for equality.
  • Extracting a portion of a string.

S. Sridevy

7/4/2022

5 of 20

Declaration of a String Variable

  • A string variable is any valid C variable name and is always declared as an array.
  • The general form of declaration of a string variable is

char string_name[size];

Eg: char city[10];

char name[30];

S. Sridevy

7/4/2022

6 of 20

Declaration of a String Variable

  • When the compiler assigns a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string. Therefore, the size should be equal to the maximum number of characters in the string plus one.

S. Sridevy

7/4/2022

7 of 20

Initialization of a String Variable

  • C permits a character array to be initialized in either of the following two forms

static char city[9] = “NEW YORK”;

static char city[9] = {‘N’, ‘E’, ‘W’, ‘ ‘, ‘Y’, ‘O’, ‘R’, ‘K’, ‘\0’};

Note : When we initialize a character array by

listing its elements, we must supply

explicitly the null terminator (‘\0’).

S. Sridevy

7/4/2022

8 of 20

Reading a word

  • The familiar input function scanf can be used with %s format specification to read in a string of characters.

Eg1: char address[15];

scanf(“%s”,address);

Note: ‘&’ is used to get the address of the variable. C does not have a string type, String is just an array of characters and an array variable stores the address of the first index location.

S. Sridevy

7/4/2022

9 of 20

Reading a Line of Text

  • It is not possible to use scanf function to read a line containing more than one word.

Eg1: char address[15];

scanf(“%s”,address);

getchar ( )

It is used to read a single character from the terminal.

ch = getchar ( );

  • We can use the getchar function repeatedly to read single character from the terminal
  • Thus an entire line of text can be read and stored in an array.

S. Sridevy

7/4/2022

10 of 20

Example

/*Program to read a line of text from terminal*/

#include<stdio.h>

void main()

{ char line[81],character; int c; c = 0;

printf(“Enter text. Press<Return>at end \n”);

do {

character = getchar();

line[c] = character;

c++;

} while(character != ‘\n’);

c = c-1;

line[c] = ‘\0’;

printf(“\n %s \n”,line);

}

S. Sridevy

7/4/2022

OUTPUT

Enter text. Press<Return>at end

Programming in C is interesting

Programming in C is interesting

11 of 20

Writing Strings

  • The format %s can be used to display an array of characters that is terminated by the null character.

Eg. printf(“%s”, name);

Arithmetic Operations on Characters

  • Whenever a character constant or character variable is used in an expression, it is automatically converted into integer value by the system.

Eg 1:

x = ‘a’;

printf(“%d \n”,x);

will display the number 97 on the screen.

S. Sridevy

7/4/2022

Eg 2:

x = ‘z’ – 1;

output :

x = 121

(ASCII of ‘z’ ie 122 – 1)

12 of 20

atoi ( )

  • It converts a string of digits into their integer values.

Syntax

x = atoi(string)

Eg:

static char ch[ ] = “1987”;

int y;

y = atoi(ch);

S. Sridevy

7/4/2022

13 of 20

String - Handling Functions

S. Sridevy

7/4/2022

Function

Action

strcat( )

Concatenates two strings

strcmp( )

Compares two strings

strcpy( )

Copies one string over another

strlen( )

Finds the length of the string

14 of 20

strcat( ) function

  • It concatenates two strings . The strcat() function joins two strings together.

Syntax

strcat( string1,string2);

S. Sridevy

7/4/2022

15 of 20

strcat( ) function

Eg 2:

static char st1[30] = “First”;

static char st2[10] = “B.Tech”;

static char st3[10] = “FoT”;

strcat(strcat(st1,st2),st3);

printf(“%s”,st1);

printf(“%s”,st2);

printf(“%s”,st3);

S. Sridevy

output :

First B.Tech FoT

B.Tech

FoT

16 of 20

strcmp( ) function

  • It compares two strings. It is used to compare two strings identified by the arguments and has a value 0 if they are equal.

Syntax

strcmp( string1,string2);

S. Sridevy

7/4/2022

17 of 20

strcmp( ) function

Eg: 1) strcmp(name1,name2);

2) strcmp(name1,”john”);

3) strcmp(“ram”, “raj”);

 

In Eg 3 , the result will be 3 . That is , ASCII value of “m” minus ASCII value of “j” (109 – 106).

S. Sridevy

18 of 20

strcpy( ) function

  • It copies one string over another. This function works almost like a string assignment operator.
  • Syntax

strcpy( string1,string2);

  • This assigns the content of string2 to string1.

S. Sridevy

7/4/2022

19 of 20

strcpy( ) function

Eg: 1) strcpy(grade, “First Class”);

2) strcpy(watercon, “GOOD”);

S. Sridevy

20 of 20

strlen( ) function

  • It finds the length of the string. This function counts and returns the number of characters in a string.

Syntax :

n = strlen(string);

Eg:

n = strlen(“INDIA”);

printf(“%d”,n);

S. Sridevy

7/4/2022