Computer Science Project Work class 10th (String Manipulation)

909

Project – 4

“String Manupulation”

A string in C is a sequence of zero are more characters followed by a NULL (‘/0’) character.It is important to preserve the NULL termination character as it is how how C defines and manages variable length string. i.e. all the C standard library function require this for successful operation.

In the C language string are defined as :

* an array of characters, or

* a pointer to a portion of memory containing ASCII characters.

All the string handling functions are prototyped in : in string .h or stdio .h standard header file so, while using any string related function, include either stdio.h or string.h.

A string is a sequence of characters. Any sequence or set of characters defined within double quotation symbols is a constant string.

Reading and Writing strings

(a) Scanf function :

One possible way to read in a string is by using ‘scanf ()’

Example :

char address [25];

scanf (“% s “, address);

(b) gets function :

A gets text just one argument a char pointer, all the name of a char array.

Example :

char name [15];

ges (name);

(c) Puts function :

A puts function is similar to gets function in the way that it takes one argument a char pointer.

This also automatically adds a new line character after printing out the string.

Example :

char student [20];

puts (student);

(d) Printf function :

sometimes puts function can be a disadvantage, so printf could be used instead.

Example :

char message [50];

printf (“Message is : %”);

Library function :

C library supports a large number of string handling functions that can be used to add a out many of the string manipulation are :

(a) Length, number of characters in the string.

(b) Concatenation, add two or more strings.

(c) Comparing, compare two strings.

(d) Substring, extract substring from a given string.

(e) Copy, copies one string over another.

To do all above operations, it is essential to include string.h library header file in the program.

Read also :

Class 10th Computer Science ka Project Work in Hindi

(1) Strlen () function :

This function counts and returns the number of characters in a string. The length does not include a null character.

sintex :n = Strlen (string);

where n is integer variable. which receive the value of length of the string.

Read also :

Class 10th Computer Science Project – 2

Program :

# include <stdio.h>

# include <string.h>

void main ()

{

char name [20];

int lenght;

printf (“Enter the string :”);

gets (name);

lenght = Strlen (name);

printf (“Number of characters = %d”. lenght);

}

(2) strcat () function :

when we combine two strings, we add the characters Afghan string to the end of another string. This process is called concatenation. The strcal () function joins two strings together.

sintex : strcat (string 1, string 2)

Program :

# include <stdio.h>

# include <string.h>

void main ()

{

char string 1 [20], string 2 [15];

printf (“Enter the first string :”);

gets (string 1);

printf (“\n%s”, strcat (string 1, string 2));

gets (string 2);

printf (“\n % s”, strcat (string 1, string 2);

}

(3) strcmp () function :

In C, we cannot directly compare the value of two strings in a condition like if (string 1== string 2).

This function returns zero, if two strings are equal.

sintex : str CMP ( string 1, string 2)

Program :

# include <stdio.h>

# include <string.h>

void main ()

{

char str 1 [20], str 2 [20];

int x;

printf (“Enter the string.”);

scanf (“% s% s”, str1, str2);

x =str cmp (str1, str2);

if (x ! = 0)

printf (“\n string are not equal.”);

else

printf (“\n strings are equal.”);

(4) strcpy () function :

str cpy () function assigns the contents of one string to another string.

syntax :strcpy (string2, string1);

Program :

# include <stdio.h>

# include <string.h>

void main ()

{

char string 1 [20], string 2 [20];

Printf (“string 2, string 1);

gets ( string 1);

strcpy ( string 2, string 1);

printf (” \n second string is : % s”, string 2);

}

Conclusion :

This project provides us the knowledge of a string and the functions using in string manipulation.

VIVA-VOCE QUESTION

Ques.1 What is the different between character and string?

Ans. Strings are enclosed within double quotes where a single character are enclosed within single quotes.

Ques.2 What is functionality of strrev () function?

Ans. This function reverses the character in a string.

Ques.3 What is strlwr ()?

Ans. strlwr () function converts all characters in a stream from uppercase to lowercase.

Ques.4 What is the difference between STR cmp () and strcmpi ()?

Ans. Both functions are used to compares two strings, but in in strcmpi()

Read also :

Class 10th Computer Science Project Work-3

LEAVE A REPLY

Please enter your comment!
Please enter your name here