[go: up one dir, main page]

0% found this document useful (0 votes)
13 views25 pages

3 Strings

The document provides a comprehensive overview of strings in programming, defining a string as an array of characters and detailing common operations such as reading, writing, concatenating, and comparing strings. It also discusses various library functions for string manipulation, including strlen, strcpy, strcmp, and strcat, along with examples of their usage. Additionally, the document covers techniques for reading multiple lines, inserting substrings, and deleting substrings from a main string.

Uploaded by

batmanflyinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

3 Strings

The document provides a comprehensive overview of strings in programming, defining a string as an array of characters and detailing common operations such as reading, writing, concatenating, and comparing strings. It also discusses various library functions for string manipulation, including strlen, strcpy, strcmp, and strcat, along with examples of their usage. Additionally, the document covers techniques for reading multiple lines, inserting substrings, and deleting substrings from a main string.

Uploaded by

batmanflyinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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];

The size determines the number of characters in the


string_name.
Example
Reading Multiple Lines
 We use third argument in cin.get() function

 cin.get(array_name, size, stop_char)

 The argument stop_char specifies the character that tells the


function to stop reading. The default value for this argument is
the newline (‘\n’) character, but if you call the function with
some other character for this argument, the default will be
overridden by the specified character.
Example
#include <iostream>
using namespace std;
int main(){
const int len = 80; //max characters in string
char str[MAX];
//string variable str
cout << "\nEnter a string:";
char str[len];
cin.get(str, len);
cout<<"\n"<<str;
return 0;
}
Length of string
#include <iostream>
using namespace std;
int main(){
char a[30];
int i, c=0;
cout<<"Enter a string:";
cin.get(a,30);
for(i=0;a[i]!='\0';i++)
c++;
cout<<"\nLength of the string '"<<a<<"' is "<<c;
return 0;
}
String concatenation
#include<iostream>
while(str2[j]!='\0')
using namespace std; {
int main() {
char str1[55],str2[25]; str1[i]=str2[j];
int i=0,j=0; j++;
i++;
cout<<"\nEnter First
}
String:";
str1[i]='\0';
cin>>str1; cout<<"\nConcatenated
cout<<"\nEnter Second String is\n"<<str1;
String:"; return 0;}
cin>>str2;
while(str1[i]!='\0')
i++;
Library functions: String Handling
functions(built-in)
These in‐built functions are used to manipulate a given string. These
functions are part of string.h header file.

 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()

• String length can be obtained by using the following function


n=strlen(string);
This function counts and returns the number of characters in
a string, where n is an integer variable which receives the
value of the length of the string. The argument may be a
string constant.

• Copying a String the Hard Way


The best way to understand the true nature of strings is to
deal with them character by character. The following program
copies one string to another character by character.
Copies a string using a for loop

#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.

 Destination may be a character array variable or a string


constant.
e.g., strcpy(city,”DELHI”);
will assign the string “DELHI” to the string variable city.

 Similarly, the statement strcpy(city1,city2);


will assign the contents of the string variable city2 to the string variable city1.
Note:- The size of the array city1 should be large enough to receive
the contents of city2.
strcpy(): Example
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[ ] = "Manipal Institute of Technology";
char str2[100];
strcpy(str2,str1);
cout << str1<<"\n"<<str2 << endl;
return 0;
}
Library function: strcmp()
The strcmp function compares two strings identified by the arguments
and has a value 0 if they are equal.

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.

e.g., strcmp(“their”,”there”); will return a value of –9 which is the numeric


difference between ASCII “i” and ASCII “r”. That is, “i” minus “r” with respect to
ASCII code is –9.

Note:- If the value is negative, string1 is alphabetically above


string2.
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[ ] = "Manipal Institute of Technology";
char str2[ ] ="Manipal Institute of Technology";
if(strcmp(str1,str2)!=0)
cout<<"strings are not equal";
else
cout <<"Two strings are equal";
return 0;
}
Library function: strcat()
The strcat function joins two strings together.
 It takes the following form:
 strcat(string1,string2);
 string1 and string2 are character arrays.

When the function strcat is excuted, string2 is appended


to a string1.

It does so by removing the null character at


the end of string1 and placing string2 from there.

The string at string2 remains unchanged.


#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[ ] = "Manipal";
char str2[] =" Institute of Technology";
strcat(str1,str2);
cout<<"concatenated string is\n"<<str1;
return 0;
}
Reading integer followed by sentences

#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

3. Compare m[i] and s[j].


 If match is found increment both control variables.
 If match is not found put m[i] into the new array n[k] and
increment i .
 Keep a flag variable to ensure that complete substring is present
in the main string

4. If the end of substring is reached update the index j to 0 and


again compare m[i] with s[j] and so on….
THANK YOU

You might also like