3 Strings
3 Strings
String Definition
A string is an array of characters.
Any group of characters (except double quote sign) defined
between double quotation marks is a constant string.
Character strings are often used to build meaningful and
readable programs.
The common operations performed on strings are
Reading and writing strings
Combining strings together
Copying one string to another
Comparing strings to another
Extracting a portion of a string ..etc.
Declaration and initialization
char string_name[size];
strlen ()
gives the length of the string. E.g. strlen(string)
strcpy ()
copies one string to other. E.g. strcpy(Dstr1,Sstr2)
strcmp ()
compares the two strings. E.g. strcmp(str1,str2)
strcat ()
Concatinate the two strings. E.g. strcat(str1,str2)
Library function: strlen()
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[ ] = "Manipal Institute of Technology";
char str2[100];
int j;
for(j=0 ; j<strlen(str1); j++)
str2[j] = str1[j];
str2[j] = '\0';
cout << str1<<"\n"<<str2 << endl;
return 0;
}
Copies a string using a for loop
The copying is done one character at a time, in the Statement
str2[j] = str1[j];
The copied version of the string must be terminated with a null.
However, the string length returned by strlen() does not include the
null.
We could copy one additional character, but it’s safer to insert the nu
explicitly. We do this with the line str2[j] = ‘\0’;
If you don’t insert this character, you’ll find that the string printed by
the program includes all sorts of weird characters following the string
you want.
The << just keeps on printing characters, whatever they are, until by
chance it encounters a ‘\0’.
Library function: strcpy()
Copying a String the Easy Way: strcpy(destination, source)
The strcpy function works almost like a string assignment
operator and assigns the contents of source to destination.
If they are not, it has the numeric difference between the first non
matching characters in the strings.
strcmp(string1,string2);
String1 and string2 may be string variables or string constants.
#include <iostream>
using namespace std;
//reading integer followed by sentences
int main(){
int n;
char s1[10],s2[10];
cout<<"Enter integer";
cin>>n;
fflush(stdin);
gets(s1);
fflush(stdin);
gets(s2);
cout<<"s1="<<s1<<endl;
cout<<"s2="<<s2<<endl;
return 0;
#include <iostream> Reading integer followed
using namespace std; by multiline input strings
int main(){
int n; char s1[10],s2[10];
cout<<"Enter integer";
cin>>n;
fflush(stdin);
cin.get(s1,10,'$');
fflush(stdin);
cin.get(s2,10,'$');
cout<<"s1="<<s1<<endl;
cout<<"s2="<<s2<<endl;
return 0;
}
Insert a substring into a given
string:
Steps to be followed:
1. Take a string, substring and position where
the substring is to be inserted in main string
as input.
2. Copy all the characters from the given
position to a temporary array.
3. Substring should be inserted at the given
position character by character to main string.
4. Now to this array copy all characters from
the temporary array.
void main()
{ while(i <= r)
char a[10]; { C Program to insert a
char b[10]; c[i]=a[i]; sub-string in to given
char c[10];
i++; main string from a given
int p=0,r=0,i=0;
} position.
s = n+r;
int t=0;
o = p+n;
int x,g,s,n,o;
// Adding the sub-string
clrscr();
for(i=p;i<s;i++)
puts("Enter First String:");
{
gets(a); x = c[i];
puts("Enter Second String:"); if(t<n)
gets(b); {
printf("Enter the position where the item has a[i] = b[t];
to be inserted: ");
t=t+1;
scanf("%d",&p);
}
r = strlen(a); a[o]=x;
n = strlen(b); o=o+1;
i=0; }
// Copying the input string into another array printf("%s", a);
getch();
https://scanftree.com/programs/c/to-insert-a-sub-string-in-to-given-main-string-using-c/
}
Delete substring
1. Take a string and its substring as input.
2. Take two control variable “i” for main string and “j” for
substring