String Processing
In computer science a strnig is any finite sequence of characters (i.e letters, numbers , symbols and punctuation marks).
String is a sequence of characters.
In C programming, array of characters are called strings.
A string is terminated by the character /0. a.k.a termination-character
The other part is called as the String literal
Operations
- Reading
- Combining / Concatenating
- Displaying
- Comparing
- Copying
- Finding length
- Extracting a portion
- Pattern Matching
String could be fixed length or variable length strings.
Variable length strings are of two types :
- Length controlled Strings
- Delimited Strings
Fixed length of strings are the ones whose size is defined while declaration of the variable.
Length Controlled
A length controlled string is a string whose length is stored as part of the string itself.
Generally the very first byte
The number of substrings for any string of length n is [n(n-1)]/2
Delimited String
The string that ends with a delimiter denoting the end of the string is called a delimited string.
General strings are delimited by NULL i .e "\0"
String libraries are available for String ADTs .
To create our own string ADT . . .
String Operations
String Length
- We know that the string is terminated by a null character ('\0').
- The string is first read from the index being initialised to zero
- The index is incremented after every read
- As soon as the index reaches the null character - the value of the index is the length of the characters
# include<stdio.h>
main(){
char str[40];
int i=0;
printf("Enter the string: \n");
gets(str);
while(str[i]!='\0'){
i++;
}
printf("\n the length of the string is: %d",i);
}
String Concatenation
Characters are stored as integers in string
Essentially as a 1-byte integer
For ex: if 'A' has to be stored as a char, it is stored as '65'
Since C read this as an ASCII values
But if the data for the variable is integers it would be stored as a plain integers , meaning
You cannot assign an int type '65' and printout 'A'
but you can do that for a char type.
P.S: Coming to the main point
Concatenation of strings is putting strings together
But since Strings are immutable we take every starting character of the second string and append it to the first string . . .
While ensuring the first string has enough capacity to store the combination of both strings combined.
#include<stdio.h>
main(){
char first[100],second[50];
int i,j;
printf("Enter the value of first string: \n");
gets(first);
printf("Enter the value of second string: \n");
gets(second);
while(first[i]!='\0'){
i++;
}
while(second[j]!='\0'){
first[i]=second[j];
i++;
j++;
}
first[i]='\0';
printf("Concatenation of two strings is: %s",first);
}
String comparision
#include<stdio.h>
main(){
char fisrt[50],second[50];
int i;
printf("Enter first string: \n");
gets(first);
printf("Enter second string: \n");
gets(second);
while(first[i]==second[i] && first[i]!='\0'){
i++;
}
if (first[i]==second[i]){
printf("The strings are not equal");
}
else{
printf("the strings are equal");
}
}
Substring
The string is a part of the string
To get the substring we require the following:
- String
- start position
- Length
stdio stands for standard input output
conio stands for console input output
#include<stdio.h>
int main(){
char string[50];
printf("Enter the string: \n");
fgets(string,50,stdin);
int s,n;
printf("Enter the starting point: \n");
scanf("%d",&s);
s=s-1;
printf("Enter the length of the substring: \n");
scanf("%d",&n);
char substring[50];
int i=0,e=s+n;
while(s!=e){
substring[i]=string[s];
s++;
i++;
}
printf("Substring: %s",substring);
return 0;
}
Indexing