ASSIGNMENT PRESENTATION – 1 
DATA STRUCTURES 
TEAM-1 
TOPIC: STRING- IMPLEMENTATION, OPERATIONS 
AND APPLICATIONS 
TEAM MEMBERS: 
1.ARUN KUMAR 
2.DIVYA 
3.GAYATHRI 
4.KAVYA 
5.MOHANA PRIYA 
6.PRASHANTH
STRING: 
 Finite sequence of characters. 
 Data type storing sequence of data values, in bytes 
according to character encoding. 
 It is null terminated character array. Size of the string is 
length of the string plus one. 
 Variable is declared to have string data type, holding 
some predetermined symbols. 
 Represented using double quotes. 
Declaration: char name[size]; 
Formal theory to represent strings: 
Let Σ be a non-empty finite set of letters of an 
alphabet. Elements of Σ are called symbols or characters. 
A string over Σ is any finite sequence of characters from Σ. 
EG: if Σ={0,1} then 01101 is a string over Σ.
IMPLEMENTATION: 
String is a variable length array of characters that is 
delimited by null character. 
They are implemented as array of bytes, characters or 
code units in order to allow fast access to characters in a 
string. 
Difference between character and string: 
String: character: 
char str[]=“H”; H  0 char str = ‘H’; H 
1. Here H is a string not a 
character and they are 
enclosed by double quotes. 
1. Here H is a character not 
a string and they are 
enclosed by single quotes. 
2. They require two memory 
location including the null 
character. 
2. It requires only single 
memory location and it 
doesn’t have null character.
Compiler assigns character string to a character 
array and automatically assigns a null character to the 
end of string. 
size of the string= length of the string + 1. 
There are two types of strings namely: 
 Mutable strings: Eg: C++, ruby. 
 Immutable strings: Eg: Java, python. 
Reading strings: 
1. Using scanf function. scanf(“%s”,str); 
2. Using gets() function. gets(str); 
3. Using getchar() and getche() funtion. getchar(ch); 
Writing strings: 
1. Using printf function. printf(“%s”,str); 
2. Using gets() function. gets(str); 
3. Using getchar() and getche() funtion. getchar(ch);
Implementing strings using pointers: 
This uses a pointer to access the elements. 
Eg: #include<stdio.h> 
void main() 
{ 
char name[ ]=“hello”, *ptr; 
ptr=name; 
while(*ptr != ‘0’) 
{ 
printf(“%s”, *ptr); 
ptr++; 
} 
} 
Output: hello
OPERATIONS: 
Searching: 
Searching is to find a particular character in a string 
or not and to return the position. 
Algorithm: 
Search(A,N,VAL,POS) 
Step 1: [INITIALIZE] SET POS= -1 
Step 2: [INITIALIZE] SET I=0 
Step 3: Repeat Step 4 while I<N 
Step 4: IF A[I] = VAL 
SET POS = I 
PRINT POS 
Go to step 6. 
Step 5: PRINT “Value not present in the string” 
Step 6: EXIT.
Sorting: 
Sorting is used to arrange the characters in an array 
using some order. 
Algorithm: 
SORTING(A,N) 
Step 1: Repeat Step 2 FOR I = 0 to N-1 
Step 2: Repeat step 3 FOR J = 0 to N-1 
Step 3: IF A[J]>A[I], then 
SWAP A[J] and A[J+1] 
[End of Inner and Outer Loop] 
Step 4: EXIT. 
Trim: String trim(String str); 
Returns a string with whitespace stripped from the 
beginning and end of the string. Whitespace characters 
are n,t,0 and plain space. It also has rtrim() and ltrim().
Insertion: 
The insertion operation inserts a string s, in the main 
text T, at Kth position. 
Algorithm: INSERT(text,pos,str) 
Step 1: [INITIALIZE] SET I = 0, J = 0 and K=0 
Step 2: Repeat step 3 to 4 while text[I] != ‘0’ 
Step 3: If I=Pos, then 
Repeat while str[K] != ‘0’ 
new_str[J]=str[K] 
SET J=J+1 and K=K+1 
Else new_str[J] = text[I] 
SET J=J+1 
Step 4: SET I = I+1 
Step 5: SET new_str[J] = ‘0’ 
Step 6: EXIT.
Deletion: 
It deletes a substring from a given text. 
Algorithm: 
Step1: [INITIALIZE] SET I=0 AND J=0 
Step 2: Repeat steps 3 to 6 while text[I] != ‘0’ 
Step 3: If I = Pos, then 
Repeat step 4 while n>=0 
SET I = 1+1 
SET N=N-1 
Step 4: SET new_str[J] = text[I] 
Step 5: SET J = J+1 
Step 6: SET I = I+1 
Step 7: SET new_str[J] = ‘0’ 
Step 8: EXIT
String Manipulation Functions: 
1.The strcat function 
Syntax: char *strcat(char *s1, char *s2); 
2.The strncat function 
Syntax: char *strncat(char *s1, char *s2, size n); 
3.The strchr function 
Syntax: char *strchr(char *s1, int c); 
4.The strrchr function 
Syntax: char *strrchr(char *s1, int c); 
5. The strcmp function 
Syntax: char *strcmp(char *s1, char *s2); 
6. The strncmp function 
Syntax: char *strncmp(char *s1, char *s2, size n);
7. The strcpy function 
Syntax: char *strcpy(char *s1,char *s2); 
8. The strncpy function 
Syntax: char *strncpy( char *s1, char *s2, size n); 
9. The strlen function 
Syntax: int strlen(char *str); 
10. The strstr function 
Syntax: char *strstr( char *s1, char *s2); 
11. The strspn function 
Syntax: int strspn(char *s1, char *s2); 
12. The strcspn function 
Syntax: int strcspn(char *s1, char *s2); 
13. The strpbrk function 
Syntax: char *strpbrk( char *s1, char *s2);
14.The strtok function 
Syntax: char *strtok( char *s1, char *delimiter); 
15. The strtol function 
Syntax: long strtol(char *str, char **end, int base); 
16.The strtod function 
Syntax: double strtod(char *str, char **end); 
Program: 
#include<stdio.h> 
#include<string.h> 
int main() 
{ 
char str1[50]= “PROGRAMMING”, str2[50]= “IN C”, *pos; 
char s1[50]=“HELLO”, s2[50]=“HEY”, s3[10], *ptr; 
char delim[3]=“,”, str[50]=“HELLO, TO, THE, WORLD”; 
long num; 
double n;
strcat(str1,str2); 
printf(“n Using strcat function: %s”, str1); 
strncat(str1,str2,2); 
printf(“n Using strncat function: %s”,str1); 
pos= strchr(str1, ‘N’); 
if(pos) 
printf(“n strchr- Character is found at position %d”, 
pos); 
else 
printf(“n strchr- Character not found”); 
pos1= strrchr(str1,’M’); 
if(pos1) 
printf(“n strrchr- last position of character is %d”, 
pos1); 
else 
printf(“n strrchr- Character not found”);
if(strcmp(s1,s2)==0) 
printf(“n strcmp- Strings are identical”); 
else 
printf(“n strcmp- Strings are not identical”); 
if(strncmp(s1,s2,2)==0) 
printf(“n strncmp- Strings are identical”); 
else 
printf(“n strncmp- Strings are not identical”); 
strcpy(s3,s1); 
printf(“n strcpy function: %s”,s3); 
strncpy(s3,s1,2); 
printf(“n strncpy function: %s”,s3); 
printf(“n Length of the string: %d”,strlen(s1));
ptr=strstr(str1,str2); 
if(ptr) 
printf(“n strstr- Substring found”); 
else 
printf(“n strstr- Substring not found”); 
printf(“n strspn- Position of character that doesnot 
match: %d”, 
strspn(str1,str2)); 
printf(“n strcspn- Position of first character that matches: 
%d”, strspn(str1,str2)); 
*ptr= strpbrk(str2,s3); 
if(ptr==NULL) 
printf(“n strpbrk- No character matches using pointer”); 
else 
printf(“n strpbrk- Character matches”); 
result=strtok(str, delim);
while(result != NULL) 
{ 
printf(“n strtok function: %s”,result); 
result= strtok(NULL,delim); 
} 
num= strtol(“65432 octal”, NULL, 8); 
printf(“strtol function: octal value is %ld”, num); 
n= strtod(“123.456abcd”,NULL); 
printf(“n strtod function: %lf”,n); 
returen 0; 
} 
Output: 
Using strcat function: PROGRAMMING IN C 
Using strncat function: PROGRAMMING IN 
strchr- Character is found at the position: 9
strrchr- Last position of character is: 7 
strcmp- Strings are not identical 
strncmp- Strings are identical 
strcpy funcion- HELLO 
strncpy function- HE 
length of the string- 5 
strstr- Substring found 
strspn- Position of the string that doesnot match: 3. 
strcspn- position of the character that matches: 8. 
strpbrk- No characters matches using pointer. 
strtok function: HELLO TO THE WORLD. 
strtol function: Octal value is 27418. 
strtod function: 123.456000
APPLICATIONS USING STRING: 
1. Pattern matching in computation biology. 
2. Text editor, Digital library and search engine. 
3. String matching through 2-Dimensional meshes
REFERENCES: 
 Reema Thareja, “Programming in C”, Oxford University 
press, 2011. 
 http://en.wikipedia.org/wiki/string_operation 
 http://www.stackoverflow.com/string
THANK YOU !!!

Team 1

  • 1.
    ASSIGNMENT PRESENTATION –1 DATA STRUCTURES TEAM-1 TOPIC: STRING- IMPLEMENTATION, OPERATIONS AND APPLICATIONS TEAM MEMBERS: 1.ARUN KUMAR 2.DIVYA 3.GAYATHRI 4.KAVYA 5.MOHANA PRIYA 6.PRASHANTH
  • 2.
    STRING:  Finitesequence of characters.  Data type storing sequence of data values, in bytes according to character encoding.  It is null terminated character array. Size of the string is length of the string plus one.  Variable is declared to have string data type, holding some predetermined symbols.  Represented using double quotes. Declaration: char name[size]; Formal theory to represent strings: Let Σ be a non-empty finite set of letters of an alphabet. Elements of Σ are called symbols or characters. A string over Σ is any finite sequence of characters from Σ. EG: if Σ={0,1} then 01101 is a string over Σ.
  • 3.
    IMPLEMENTATION: String isa variable length array of characters that is delimited by null character. They are implemented as array of bytes, characters or code units in order to allow fast access to characters in a string. Difference between character and string: String: character: char str[]=“H”; H 0 char str = ‘H’; H 1. Here H is a string not a character and they are enclosed by double quotes. 1. Here H is a character not a string and they are enclosed by single quotes. 2. They require two memory location including the null character. 2. It requires only single memory location and it doesn’t have null character.
  • 4.
    Compiler assigns characterstring to a character array and automatically assigns a null character to the end of string. size of the string= length of the string + 1. There are two types of strings namely:  Mutable strings: Eg: C++, ruby.  Immutable strings: Eg: Java, python. Reading strings: 1. Using scanf function. scanf(“%s”,str); 2. Using gets() function. gets(str); 3. Using getchar() and getche() funtion. getchar(ch); Writing strings: 1. Using printf function. printf(“%s”,str); 2. Using gets() function. gets(str); 3. Using getchar() and getche() funtion. getchar(ch);
  • 5.
    Implementing strings usingpointers: This uses a pointer to access the elements. Eg: #include<stdio.h> void main() { char name[ ]=“hello”, *ptr; ptr=name; while(*ptr != ‘0’) { printf(“%s”, *ptr); ptr++; } } Output: hello
  • 6.
    OPERATIONS: Searching: Searchingis to find a particular character in a string or not and to return the position. Algorithm: Search(A,N,VAL,POS) Step 1: [INITIALIZE] SET POS= -1 Step 2: [INITIALIZE] SET I=0 Step 3: Repeat Step 4 while I<N Step 4: IF A[I] = VAL SET POS = I PRINT POS Go to step 6. Step 5: PRINT “Value not present in the string” Step 6: EXIT.
  • 7.
    Sorting: Sorting isused to arrange the characters in an array using some order. Algorithm: SORTING(A,N) Step 1: Repeat Step 2 FOR I = 0 to N-1 Step 2: Repeat step 3 FOR J = 0 to N-1 Step 3: IF A[J]>A[I], then SWAP A[J] and A[J+1] [End of Inner and Outer Loop] Step 4: EXIT. Trim: String trim(String str); Returns a string with whitespace stripped from the beginning and end of the string. Whitespace characters are n,t,0 and plain space. It also has rtrim() and ltrim().
  • 8.
    Insertion: The insertionoperation inserts a string s, in the main text T, at Kth position. Algorithm: INSERT(text,pos,str) Step 1: [INITIALIZE] SET I = 0, J = 0 and K=0 Step 2: Repeat step 3 to 4 while text[I] != ‘0’ Step 3: If I=Pos, then Repeat while str[K] != ‘0’ new_str[J]=str[K] SET J=J+1 and K=K+1 Else new_str[J] = text[I] SET J=J+1 Step 4: SET I = I+1 Step 5: SET new_str[J] = ‘0’ Step 6: EXIT.
  • 9.
    Deletion: It deletesa substring from a given text. Algorithm: Step1: [INITIALIZE] SET I=0 AND J=0 Step 2: Repeat steps 3 to 6 while text[I] != ‘0’ Step 3: If I = Pos, then Repeat step 4 while n>=0 SET I = 1+1 SET N=N-1 Step 4: SET new_str[J] = text[I] Step 5: SET J = J+1 Step 6: SET I = I+1 Step 7: SET new_str[J] = ‘0’ Step 8: EXIT
  • 10.
    String Manipulation Functions: 1.The strcat function Syntax: char *strcat(char *s1, char *s2); 2.The strncat function Syntax: char *strncat(char *s1, char *s2, size n); 3.The strchr function Syntax: char *strchr(char *s1, int c); 4.The strrchr function Syntax: char *strrchr(char *s1, int c); 5. The strcmp function Syntax: char *strcmp(char *s1, char *s2); 6. The strncmp function Syntax: char *strncmp(char *s1, char *s2, size n);
  • 11.
    7. The strcpyfunction Syntax: char *strcpy(char *s1,char *s2); 8. The strncpy function Syntax: char *strncpy( char *s1, char *s2, size n); 9. The strlen function Syntax: int strlen(char *str); 10. The strstr function Syntax: char *strstr( char *s1, char *s2); 11. The strspn function Syntax: int strspn(char *s1, char *s2); 12. The strcspn function Syntax: int strcspn(char *s1, char *s2); 13. The strpbrk function Syntax: char *strpbrk( char *s1, char *s2);
  • 12.
    14.The strtok function Syntax: char *strtok( char *s1, char *delimiter); 15. The strtol function Syntax: long strtol(char *str, char **end, int base); 16.The strtod function Syntax: double strtod(char *str, char **end); Program: #include<stdio.h> #include<string.h> int main() { char str1[50]= “PROGRAMMING”, str2[50]= “IN C”, *pos; char s1[50]=“HELLO”, s2[50]=“HEY”, s3[10], *ptr; char delim[3]=“,”, str[50]=“HELLO, TO, THE, WORLD”; long num; double n;
  • 13.
    strcat(str1,str2); printf(“n Usingstrcat function: %s”, str1); strncat(str1,str2,2); printf(“n Using strncat function: %s”,str1); pos= strchr(str1, ‘N’); if(pos) printf(“n strchr- Character is found at position %d”, pos); else printf(“n strchr- Character not found”); pos1= strrchr(str1,’M’); if(pos1) printf(“n strrchr- last position of character is %d”, pos1); else printf(“n strrchr- Character not found”);
  • 14.
    if(strcmp(s1,s2)==0) printf(“n strcmp-Strings are identical”); else printf(“n strcmp- Strings are not identical”); if(strncmp(s1,s2,2)==0) printf(“n strncmp- Strings are identical”); else printf(“n strncmp- Strings are not identical”); strcpy(s3,s1); printf(“n strcpy function: %s”,s3); strncpy(s3,s1,2); printf(“n strncpy function: %s”,s3); printf(“n Length of the string: %d”,strlen(s1));
  • 15.
    ptr=strstr(str1,str2); if(ptr) printf(“nstrstr- Substring found”); else printf(“n strstr- Substring not found”); printf(“n strspn- Position of character that doesnot match: %d”, strspn(str1,str2)); printf(“n strcspn- Position of first character that matches: %d”, strspn(str1,str2)); *ptr= strpbrk(str2,s3); if(ptr==NULL) printf(“n strpbrk- No character matches using pointer”); else printf(“n strpbrk- Character matches”); result=strtok(str, delim);
  • 16.
    while(result != NULL) { printf(“n strtok function: %s”,result); result= strtok(NULL,delim); } num= strtol(“65432 octal”, NULL, 8); printf(“strtol function: octal value is %ld”, num); n= strtod(“123.456abcd”,NULL); printf(“n strtod function: %lf”,n); returen 0; } Output: Using strcat function: PROGRAMMING IN C Using strncat function: PROGRAMMING IN strchr- Character is found at the position: 9
  • 17.
    strrchr- Last positionof character is: 7 strcmp- Strings are not identical strncmp- Strings are identical strcpy funcion- HELLO strncpy function- HE length of the string- 5 strstr- Substring found strspn- Position of the string that doesnot match: 3. strcspn- position of the character that matches: 8. strpbrk- No characters matches using pointer. strtok function: HELLO TO THE WORLD. strtol function: Octal value is 27418. strtod function: 123.456000
  • 18.
    APPLICATIONS USING STRING: 1. Pattern matching in computation biology. 2. Text editor, Digital library and search engine. 3. String matching through 2-Dimensional meshes
  • 19.
    REFERENCES:  ReemaThareja, “Programming in C”, Oxford University press, 2011.  http://en.wikipedia.org/wiki/string_operation  http://www.stackoverflow.com/string
  • 20.