Programming in C - Fundamental Study of Strings

ChandrakantDivate1 52 views 22 slides May 10, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Programming in C - Fundamental Study of Strings


Slide Content

Strings Introduction Reading and displaying strings Passing strings to function String handling functions C. P. Divate

Str i ng s a r e ar r a y of cha r ac t e r s i . e. th e y a r e ar r an g ed on e a f t er another in Thus , a cha r ac t er ar r a y i s c al l ed cha r ac t e r s memory. string. E ach c h a r ac t er within the s tri n g i s s t o r ed within one element of the array successively. A s tring i s a l w a y s t ermin a t ed b y a null character (i.e. slash zero \0 ). Introduction

Ope r a t i on s per f o rm e d on include: Reading and writing strings Copying one string to another Combining strings together Comparing strings for equality Extracting a portion of a string cha r ac t er s tr i n g s Arrays and Strings…

A string variable is declared as an array of characters. Syntax: char string_name[size]; E.g. char name[20]; When the compiler assigns a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string

Strings are initialized in either of the following two forms: char name[4]={‘R’,‘A’,‘M’, ‘\0’}; char name[]={‘R’,‘A’,‘M’, ‘\0’}; char name[4]=“RAM”; char name[]=“RAM”; When we initialize a character array by listing its elements, the null terminator or the size of the array must be provided explicitly. Initializing String Variables R A M \0 name[0] name[1] name[2] name[3]

Reading and displaying Strings It can be done manually.

Using printf() and scanf()

Using gets() and puts()

Passing String to function

String handling functions Strings need to be manipulated by programmer. It can be done manually but is time consuming.

#include <stdio.h> #include <conio.h> void main() { char input_string[50]; int i=0, length=0; clrscr(); printf("\nEnter your text:\t"); gets(input_string); w hil e( in p ut_ s tr i ng [i]! = ' \0') { l e ngt h ++ ; i++; } printf("\nThe length of your text is: %d character(s)", length); getch(); } Counting length of the string

#include <stdio.h> #include <conio.h> void main() { char copy[50], paste[50]; int i; clrscr(); printf("\nEnter your name (to copy):\t"); gets(copy); for(i=0;copy[i]!='\0';i++) { paste[i]=copy[i]; } paste[i]='\0'; printf("\nThe name is (pasted as):\t"); puts(paste); getch(); } Copying one string to another

There are various string handling functions define in string.h some of them are:

void main() { char input_string[50]; int length; clrscr(); printf("\nEnter your text:\t"); gets(input_string); length=strlen(input_string); printf("\nThe length of your text is: %d character(s)", length); getch(); } 14

void main() { char copy[50], paste[50]; int i; clrscr(); printf("\nEnter your name (to copy):\t"); gets(copy); strcpy(paste, copy); printf("\nThe name is (pasted as):\t"); puts(paste); getch(); } 15

void main() { char first_name[30]=“College " ; char middle_name[]=" of Applied"; char last_name[]=" Business"; clrscr(); s t rca t ( f i rs t _ n am e , m iddle_ n am e ); puts(first_name); strcat(first_name,last_name); puts(first_name); getch(); } 16

void main() { char str1[30],str2[40]; int diff; clrscr(); printf("Enter first string:\t"); gets(str1); printf("\nEnter second string:\t"); gets(str2); diff=strcmp(str1, str2); if(diff>0) printf("\n%s is greater than %s by ASCII value difference %d", str1, str2, diff); else if(diff<0) printf("\n%s is smaller than %s by ASCII value difference %d", str1, str2, diff); else printf("\n%s is same as %s", str1, str2); getch(); } 17

void main() { char string[25]; clrscr(); printf("\nInput string to be reversed:"); gets(string); strrev(string); printf("\nThe reversed string is: %s", string); getch(); } 18

String is array of characters. Thu s a n ar r a y of s tring i s 2 -D ar r a y of characters. E.g. char names[5][10]; Here, names[5][10] means 5 names having 10 characters each. 19 Arrays of Strings

Classwork WAP to read name of 5 persons using array of strings and display them WAP to sort name of 5 persons in alphabetical order

void main() { char names[5][10]; int i; clrscr(); printf("\nEnter name of 5 persons:"); for(i=0;i<5;i++) scanf("%s", names[i]); printf("\nThe names are:"); for(i=0;i<5;i++) printf("\n%s", names[i]); getch(); } 21

void main() { char names[5][10],temp[10]; int i, j; clrscr(); printf("\nEnter name of 5 persons:"); for(i=0;i<5;i++) gets(names[i]); for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(strcmp(names[i], names[j])>0) { strcpy(temp, names[i]); strcpy(names[i], names[j]); strcpy(names[j], temp); } } } printf("\nNames in ascending order:\n"); for(i=0;i<5;i++) put s ( na m e s [i ] ); getch(); } 22
Tags