Vous êtes sur la page 1sur 19

ASSIGNMENT ON C LANGUAGE

TOPIC:SUBMITTED TO:Dr.NAVDEEP SINGH SUBMITTED BY: Rajdeep Kaur (5070)

STRINGS
In c language, the group of character, digit and symbols enclosed with in double quotation mark are called strings. String is always declared as character array or we can say that character array is called string. A string is a collection of characters terminated by a null character \0.the null character indicates the end of string. Each character that composes a string is stored as one-dimensional char type array in contiguous memory location Defining string variables:The string variables which are basically array of characters can be defined using the following syntax, Char string variable [size]; The string variable is actually the name of array and size represents the maximum number of characters that can be stored and it must be a positive integer value. e.g: main ()
2

{ char name [20]; printf (enter the name: \n); scanf (%s, name); printf (\n name entered was: %s, name); } Traversing strings:Once the string is entered you may sometimes need to perform operations on individual characters of the string. Such an operation in which individual character of a string is handled one by one is known as traversing a string. Here, we use especially for loops as they are easy to handle. Prog: To print the name and then count the number of characters. #include<stdio.h> #include<conio.h> main () { int i, count=0; char a [30]; printf(\n enter your name:); scanf(%s,a);
3

for(i=0;a[i]=\0;i++) { count++; } printf(\n no. of characters are: %d,count); getch(); } Standard library string handling functions:1.Strlen ():-This function returns the number of characters in a string, not counting the terminating NULL character. For example: consider z is a int type variable, the statement, z=strlen(s1. will store 8 in z, if char s[]=ANSHUMAN; Prog: To calculate the length of the string using library function. #include<stdio.h> #include<string.h> main() { char name[30]; int i; clrscr();
4

printf(\n enter any name=); gets(name); i=strlen(name); printf(\n length of string=%d,i); getch(); } Prog: To calculate the length of string without using library function. #include<stdio.h> #include<conio.h> main () { char name[30]; int count=0,i; clrscr(); printf(\n enter the name=); gets(name); while(name[i]!=\0); { count++; i++; } printf (length of string=%d,count); getch (); }
5

2.Strcpy():-we cannot copy one string to another directly because c language does not provide any operators for strings.this function copies the string s2 to string s1 stopping after terminating NULL character has been moved.it returns the resultant string s1.e.g,consider the following strings char s1[]=Hello; char s2[]=Everybody; On executing statement, strcpy(s1,s2); Now string s1 changed to Hello Everybody. /*WAP to copy a string to another using library function strcpy()*/ #include<stdio.h> #include<string.h> void main() { char a[40],n[40]; clrscr(); printf(\n enter a string=); gets(a); strcpy(n,a); printf(String after copying=); puts(a); getch(); } 2./*wap to copy a string to another without using library function*/
6

#include<stdio.h> #include<conio.h> void main() { char str1[20],str2[20]; int i; clrscr(); printf(enter any string=); scanf(%s,&str1); for(i=0;str1[i]!=\0;i++) str2[i]=str1[i]; } Str2[i]=\0; printf(String before copying:); printf(%s,str1); printf(\n String after copying:); printf(%s,str2); getch(); } 3.Strcmp():-This function work following ways: 1.This function compares two character strings. 2.This function starts comparing both strings from the first character and continuing with successive characters until the corresponding character differ or until the end of the string is reached. Syntax: strcmp(str1,str2);
7

If str1 is less than str2,then it returns a value that is<0. If str1 is equal to str2,then it returns a value that is=0.

If str1 is greater than str2,then it returns a value that is >0. e.g: char city[30]=MADRAS char town[30]=MANGALORE strcmp(city,town); it returns the value less than 0 as the result,at the first mismatching of lettersDand N. /*WAP to arrange names in ascending order using library function*/ #include<stdio.h> #include<string.h> void main() { char city[5][20],temp[20]; int i, j; clrscr(); printf(enter the name of cities\n); for(i=0;i<5;i++) { scanf(%s,&city[i]); } printf(\n sorted list is=); for(i=0;i<5;i++)
8

{ for(j=1;j<5;j++) { If strcmp(city[j-1],city[j]>0) { strcpy(temp,city[j-1]); strcpy(city[j-1],city[j]); strcpy(city[j],temp); } } } for(i=0;i<5;i++) printf(\n %s,city[i]); getch(); } 2./*wap to compare two strings and print the locations of unmatched character and total number of matched character. #include<stdio.h> #include<conio.h> #include<string.h> main() { char a[20],b[20],count=0,loc; int 11,12,end,i,n; clrscr(); printf(\n enter first string:); scanf(%s,a);
9

printf(\n enter second string:); scanf(%s,b); 11=strlen(a); 12=strlen(b); if(11>12) end=11; else end=12; for(i=0;i<end;i++) if(a[i]==b[i]) { count++; continue; } else { loc=I; printf(\n unmatched loc:%d,loc+1); } printf(\n matches is %d out of %d,count,end); getch(); } 4.Strcat():-the purpose of this string handling function strcat() is to be concatenate or combine two different strings together.this function appends the character of string2 at the string1. Syntax: strcat(string1,string2);
10

Here string1 is combined with string2 and result will be stored in string1.e.g: suppose string1 is Hello and string2 is Dear,then after applying the strcat() function,the result will be Hello Dear and it will be stored in string1.the representation can be done by procedures as: char string1[20],string2[20]; printf(enter the strings:); scanf(%s %s,string1,string2); strcat(string1,string2); printf(%s,string1); /*wap to concatenate two strings using library function*/ #include<stdio.h> #include<string.h> main() { char a[10]=Raman; char b[10]=Hi; clrscr(); strcat(a,b); printf(\n first string=%s,a); printf(\n second string=%s,b); getch(); } /*wap to concatenate two strings without using library function*/ #include<stdio.h>
11

#include<conio.h> main() { Int I,j,counter=0; char str1[20],str2[20]; printf(enter first string=); scanf(%s,&str1); printf(\n enter the second string=); scanf(%s,&str2); for(i=0;str1[i]!=\0;i++) ++counter; for(j=counter,i=0;str2[i]!=\0;j++,i++) str1[j]=str2[i]; str1[j]=\0; printf(\n strings after concatenation=%s,str1); getch(); } Strrev():The purpose of this function is to reverse a string i.e. function reverse all the character of a string expect of null terminating character.this function takes string as its single (only one) argument. Here the first character becomes last and last becomes first. It is very useful to find whether a string is palindrome or not. Here madam is palindrome string ,because after reversing the string using this
12

function, the resultant string remains same as original. Strrev(st); Here the st is a string variable (character type). For eg. If you have a string maan stored in a string string variable st,then after using the function as: Strrev(st); Printf(reversed string is %s,st); \*w.a.p. to reverse a string to use a string function; #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[80]; clrscr(); printf(\n enter a string please:); gets(a); printf(\n string before reversed: %s, a); printf(\n string after reversing: %s, strrev(a)); getch(); } \*w.a.p. to reverse a string without using string function*/ #include<stdio.h> #include<conio.h> #include<string.h> void main()
13

{ int i,j,counter=0; char str1[20],str[20]; clsrcr(); printf(enter any string:); scanf(%s,&str1); for(i=0;str[i]!=\0;i++) ++counter; for(i=counter-1,j=0;i>=0;i--,j++) str2[j]=str[i]; str2[j]=\0; printf(\n the string you have entered is:%s,str1); printf(\n reverse of a string is:%s,str2); getch(); } Strlwr(): This function converts the uppercase letter (A to Z)in a string to lowercase. It returns pointer to string. Char *strlwr (char *s) \*w.a.p. to show the concept of strlwr() function:*/ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[30]; clrscr();
14

printf(\n enter any character in upper case=); scanf(%s,&str); printf(string in lower case=%s, strlwr(str)); getch (); } Strupr():This fuction is just reverse of above. It converts the lowercase letters letters in a string to uppercase letters. It also returns pointer to a string. Char *strupr (char *s) \*w.a.p to show the concept of strupr function.*/ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[30]; clrscr(); printf(\n enter any character in upper case=); scanf(%s,&str); printf(string in lower case=%s, strlwr(str)); getch (); }

15

PASSING ARRAY TO FUNCTION To pass an array to a function, the array name must be specified without brackets, as an actual argument within function call. The corresponding formal argument is written in the same manner; through it must be declared as an array within formal declaration. The size of array is not specified in formal argument declaration, but the empty pair of braces must be written with array name. The second argument is the number of elements that an array has so that the function must know the number of elements as an array can use. \*w.a.p. to show the concept of passing array of function*/ #include<stdio.h> #include<conio.h> void main () { int a[50],n,i,big(int[],int); clrscr(); printf(\n enter the size of array); scanf(%d,&n); printf(\n enter the elements of array); for(i=0;i<n;i++) { scanf(%d,&a[i]); }
16

Printf(\n biggest elements is,&big(a,n)); Getch(); } Into big(into x[],into m) { Into bi,I; bi=x[0]; for(i=1;i<m;i++) { If(x[i]>bi) { bi=x[i]; } } Return(bi); } PASSING STRING TO FUNCTION The method of passing string to a function is similar as of passing array to a function as strings are referred as character arrays: The followed program shows how strings are passed to a function. #include<stdio.h> #include<conio.h> #include<string.h> Void f1(char[],into); Void main()
17

{ Char str[]=hello friends; Clrscr(); I=strlen(str); Printf(\n contents of strings=%s,str); F1(str,i); Printf(\n contents of string=%s,str); Getch(); } Void f1(char s[],int m) { Printf(\n reversed string=%s,strrev(s)); Printf(\n length of string=%d,m); }
DIFFERENCE B/W RECURSION AND ITERATION

ITERATION 1.it uses repetition Structure. 2.it explicitly uses a

RECURSION 1.it uses selection structure. 2.it uses repetition through Repeated fun. Calls

Repetition structure 3.it terminates when the 3.it termination when a Loop continuation condt. Base is recognized. Fails. 4.Infinite loop occurs 4.infinite loop occurs When loop continuation when the recursion

18

Condition never becomes false.

step does not reduce The problem during Each recursive calls. 5.Overheaded of repeated 5.recursion is expensive Fun. Calls and memory in both processor time Wastage is not the case and memory space, In itration,because itration because each recursive Normally occurs within a calls causes another Fun. Copy of the fun. To be Created.

19

Vous aimerez peut-être aussi