Unit 1 Strings
Unit 1 Strings
String Processing
Suyash Bhardwaj
Dept. of Computer Science & Engineering
Faculty of Engineering & Technology
Gurukul Kangri University, Haridwar
1
In this Unit
Structure of string storage: fixed
length, variable length, linked list storage.
String operations:
Indexing,concatenation, length.
Applications of string processing:
Insertion, deletion, pattern matching, text
formation.
2
Character Strings
A sequence of characters is often referred to as
a character “string”.
A string is stored in an array of type char
ending with the null character '\0 '.
3
Character Strings
A string containing a single character takes up 2
bytes of storage.
4
Character Strings
5
Character Strings
6
Character vs. String
8
Length of string
#include <stdio.h>
int main()
{
char s[1000],i;
printf("Enter a string: ");
scanf("%s",s);
for(i=0; s[i]!='\0'; ++i);
printf("Length of string: %d",i);
return 0;
}
9
#include<stdio.h> {
#include<stdlib.h> count++;
struct node current = current->next;
{ }
int data; return count;
struct node* next; }
}; int main()
void push(struct node** head_ref, int {
new_data) struct node* head = NULL;
{ push(&head, 1);
struct node* new_node = push(&head, 3);
(struct node*) malloc(sizeof(struct push(&head, 1);
node)); push(&head, 2);
new_node->data = new_data; push(&head, 1);
new_node->next = (*head_ref); printf("count of nodes is %d",
(*head_ref) = new_node; getCount(head));
} return 0;
}
int getCount(struct node* head)
{
int count = 0; // Initialize count
struct node* current = head; // Initialize
1 current
0 while (current != NULL)
Example 1
message1: H e l l o w o r l d \0
char message2[12];
cin >> message2; // type "Hello" as input
message2: H e l l o \0 ? ? ? ? ? ?
1
1
Example 2: String I/O
String can be input using the extraction operator >>, but
one or more white spaces indicates the end of an input
string.
char A_string[80], E_string[80];
cout << "Enter some words in a string:\n";
cin >> A_string >> E_string;
cout << A_string << E_string
<< “\nEND OF OUTPUT\n";
Output:
Enter some words in a string:
This is a test.
Thisis
1 END OF OUTPUT
2
getline
char A_string[80];
cout << "Enter some words in a string:\n";
//80 is the size of A_string
cin.getline(A_string, 80);
cout << A_string << “\nEND OF OUTPUT\n";
Output:
Enter some words in a string:
This is a test.
This is a test.
END OF OUTPUT
1
4
Example 4: getline Example
Output:
Enter some words in a string:
This is a test.
This# is a te
END OF OUTPUT
1
5
Example 5: getline Example
Output:
Enter a name in the form <last,first>:
Chan,Anson
Here is the name you typed:
|Anson Chan|
1
6
1
7
Ex. 6: String Copy Function in <cstring>
void strcpy(char dest[], const char
src[]); //copies string src into string dest
example:
char name1[16], name2[16];
strcpy(name1,"Chan Tai Man");
name1:
C h a n T a i M a n \0 ? ? ?
strcpy(name2,"999999999999999");
name2:
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 \0
strcpy(name2,name1);
name2:
1 C h a n T a i M a n \0 9 9 \0
8
Example 7: strcpy in <cstring>
#include <iostream>
#include <cstring>
using namespace std;
int main (){
char string_1[6] = "Short"; // character strings
char string_2[17] = "Have a Nice Day";
char string_3[6] = "Other";
strcpy(string_1, string_2);
return 0;
}
1
9
Ex. 8: String Length Check Function in <cstring>
2
0
Ex. 9: String Length Check Function Example
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char string_1[5] = "ABCD", string_2[10]="123456789";
cout << "String 1 = " << string_1 << endl;
cout << "String 2 = " << string_2 << endl;
strncpy(string_1,string_2,strlen(string_1));
cout << "After copying, string 1 = " << string_1<<endl;
return 0;
}
//output:
String 1 = ABCD
String 2 = 123456789
2
After
1 copying, string 1 = 1234
Ex. 10: String Length Check Function in <cstring>
2
3
Ex. 10: String Copy and String Length Check
//Uses string.h:
void string_copy(char target[], const char source[],
int target_size)
{ int index;
int new_length = strlen(source);
if (new_length > (target_size))
new_length = target_size ;
Output:
This is rather long.STRING ENDS HERE.
HelloSTRING ENDS HERE.
This is raSTRING ENDS HERE.
2
5
String Comparison
int strcmp(char s1[], char s2[]);
/*compares strings s1 and s2, returns
< 0 if s1 < s2
= 0 if s1 == s2 (i.e. strcmp returns false)
> 0 if s1 > s2
*/
int strncmp(char s1[], char s2[], int limit);
/* Same as strcmp except that at most limit
characters are compared. */
2
6
String Comparison
int comp102_strncmp(char s1[], char s2[],
int limit)
{
for (int i=0; i < limit; i++){
if (s1[i] < s2[i])
return -1;
if (s1[i] > s2[i])
return 1;
}
return 0;
2
} 7
2
8
Ex. 11: String Comparison Examples
2
9
Some Common Errors
3
0
Ex. 12: Some Common Errors
3
1