0% found this document useful (0 votes)
18 views16 pages

9 Strings

Uploaded by

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

9 Strings

Uploaded by

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

Strings

What is String
• 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 '.
• A string containing a single character takes up 2 bytes of storage
Character vs. String
• A string constant is a sequence of characters enclosed in
double quotes.
• For example, the character string:
char s1[2]="a"; //Takes two bytes of storage.
s1:
a \0
• On the other hand, the character, in single quotes:
char s2= `a`; //Takes only one byte of storage.
s2:
a
String Declaration
Format :
char stringname [size];

Example:
char s[10];
String Initialization
Complile time initialization
char s[10]=“SIKKIM”; OR char s[10]={‘S’, ’I’, ’K’, ’K’, ‘I’ ’M’, ’\0’};

S I K K I M \0

Char s[15] = “SIKKIM MANIPAL”


S I K K I M M A N I P A L \0

Length of the String : No of characters in the array (less the Null Character).
Run time initialization
char s[10];
scanf(“%s”, s); → reading terminates whenever
blank space is encountered
Use header file “string.h”
gets (s); → seamless reading

Display of string
printf(“%s”, s); → print all contents in string s
char s[6]; 0 ‘T’
int i; 1 ‘H’
2 ‘E’
for (i=0; i<6; i++)
3 ‘‘
{ 4 ‘F’
scanf(“%c”, &s[i]); 5 ‘\0’
}
Problem with this approach: The entire array needs to be populated

Best Solution for reading String – gets(array_name);


String Operations
Reading & Displaying Strings
Count the characters in the string
Copying of Strings
Combining of Strings
Comparing of strings
Read & Display String
#include<stdio.h> Assignment:
Perform read operation without using
#include<string.h> gets() function

Approach:
main()
{ • Use loop to read characters
char a[100]; and fill in the array
printf(“Enter String”);
• Terminate reading when some
gets(a); character (say asterisk *) is
printf(“%s”, a); encountered.

} • Replace * with \0

Input: S M I T I N D \0
Output: SMIT IND
Implementation of gets()
main() I Ch ARRAY Test
{ Condi
char a[100], ch; tion
int i=0; 0 N N N0
printf(“Enter text data and press ‘*’ at end);
1 E NE NO
// Reading
2 W NEW NO
do
{ 3 space NEW b NO
ch=getchar(); 4 S NEW S NO
a[i] = ch;
5 K NEW SK NO
i++;
6 Y NEW SKY NO
} while (ch != ‘*’);
i=i-1; 7 * NEW SKY* YES
a[i]=‘\0’; 8 -- --- ---
// Display the string
‘i’ will change to 7
printf (“%s”, a);
‘\0’ will override *
}
N E W S K Y \0
Count the characters in the string
strlen (name_of_string); Strlen() returns Integer Value

• String Length – No of Characters in a string (less the


NULL character)
Using built-in function Without Using built-in function
#include <stdio.h> #include <stdio.h>
#include<string.h> int main() {
int main() {
char s[100] = "SMIT";
char s[100] = "SMIT";
int i;
int i;

i=strlen(s); for (i = 0; s[i] != '\0’; i++);

printf("Length of the string: %d", i); printf("Length of the string: %d", i);
return 0; return 0;
} }
Output: 4
Copying of Strings
Str1 S M I T \0
Copy the content
Str2 S M I T \0

Built-in Function Without Built-in Function

strcpy (destString, srcString); Approach:


• Copy characters in appropriate
main() positions.
{ • Insert NULL character in the
char s1[100], s2[100]; target array.
gets(s1); for (i = 0; s1[i] != '\0'; ++i) {
strcpy(s2,s1); s2[i] = s1[i];
printf(“%s”, s2); }
} s2[i] = '\0';
Combining Strings (Concatenation)
S1 S2
S M I T \0 I N D I A \0
Combined string
S M I T I N D I A \0

Built-in Function
strcat (destString, SrcString); Without Built-in Function
for(i=0; s1[i]!='\0'; i++)
main() { {
char s1[10], s2[100]; }
gets(s1);
gets(s2); for(j=0; s2[j]!='\0'; j++)
strcat(s2,s1); {
} s1[i+j]=s2[j];
S1 A B \0 }
s1[i+j]='\0';
S2 P Q A B \0
Combining strings contd..
S1 S2
S M I T \0 I N D I A \0
S3
S M I T I N D I A \0

When i  4
for(i=0; s[i] != ‘\0’; i++) Condition – Violated
{ Copying from S1 to S3 does not take
s3[i]=s1[i]; place
} S[4]  ‘ ‘
s3[i]= ‘ ‘ ;
for(j=0;s2[j]!=‘\0’;j++) Now i stands at 4
{
s3[i+j+1]=s2[j]; When j  0
Copying from S2 to S3 takes place
}
from position 5 at s3
s3[i+j+1]=‘\0’; This continues.
Comparing Strings
• Built in function
• strcmp(string1, string2);

• Compares character by character and returns as-


• Less than 0: if first string is less than second
• Equal to 0: if both are equal
• Greater than 0: if second string is greater than first

ASCII Code S1→ABC


A → 65 S2→ ABD
B → 66 strcmp(S1,S2) → O or +ve or –ve

.. A→ 65 C→ 67
B→ 66 D→ 68
String Compare (Without Built-in)
i = 0;
#include <stdio.h> // while s1 is equal to s2
int main() while (s1[i] == s2[i] && s1[i] != '\0')
{ i++;
char s1[100], s2[100]; if (s1[i] > s2[i])
int i; printf("s1 is greater than s2");
else if (s1[i] < s2[i])
printf("s1 is less than s2");
printf("\nEnter two strings :"); else
gets(s1); printf("s1 is equal to s2");
gets(s2);
return 0;
}
Other string functions
• strcmpi() – compares two strings without case
sensitivity.
• strlwr()- converts to lower case
• strrev(): Reverses all characters in the string less
NULL character
• Assignment: List all string built in functions along
with their syntax and purpose.

Program exercise:

• Check if string is palindrome


• Compare two strings without using built-in function strcmp()

You might also like