0% found this document useful (0 votes)
12 views63 pages

Chapter 6 Strings-1

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)
12 views63 pages

Chapter 6 Strings-1

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/ 63

Strings

Subject Code: CSE101R02


Subject Name: Problem Solving & Programming in C

Dr. A. Joy Christy


AP III/SoC
SASTRA Deemed to be University, Thanjavur
Introduction

• A string is a null-terminated character array

• Means, after the last character, a null character (‘\0’) is stored

• Signifies the end of character array

• char str[]=“HELLO”;

0 1 2 3 4 5
H E L L O \0

• Compiler
• assigns string to character array
• Automatically appends a null character to the end of the string
• Therefore, the size should be equal to maximum number of characters in the string + 1
• The above declaration is illegal

• Generate a compile time error for two reasons

• Array is initialized with more elements than it can store

• Initialization cannot be separated from declaration


Reading Strings

• String can be read by using three ways

• Using scanf() function

• Using gets() function

• Using getchar(), getch(), getche() function repeatedly


Using scanf()

• Ex:
char str[100];
scanf(“%s”,str) ( does not require ampersand before variable name)

• Pitfall

• Terminates on a blank space

• Ex:
char str[100];
scanf(“%s”,str)

• Hello world

• str will contain only Hello


Using gets()

• Ex:
char str[100];
gets(str);

• Overcomes the drawback of scanf()

• str is automatically terminated with a null character


Using getchar()

• You have to deliberately append the characters with a null character


Writing Strings

• String can be displayed on screen using three ways

• Using printf() function

• Using puts() function

• Using putchar() function repeatedly


Using printf()

• Ex:
printf(“%s”,str)

• May use width and precision along with %s

• Width minimum output field width

• If string is short, extra space is either left padded or right padded

• Prints only the first 3 characters in a total of five characters

• String left justified


Using puts(str)

• Overcomes the drawback of printf()

• Writes a line of output on the screen


Using putchar()

• Repeatedly prints a sequence of single characters


• Will print first p characters of str in the field width w
sprintf() function

• Output is written to a memory than to a standard output


Suppressing Input

• scanf() can read field without assigning it to any variable

• Time can be read 9:05

• : would be read, but not assigned to anything


Using a scanset

• Defines set of characters which may be read and assigned to the corresponding string

• Defined by placing the characters inside square brackets [], prefixed with a %

• scanf()

• continue to read characters

• Put them in a string

• Until it encounters a character that is not specified in the scanset


• The code will stop accepting a character as soon as user enters a character that is not a
vowel
• str, accept characters other than those specified in the scanset

• It will accept a non-vowel character

• will accept str including ^ and [

• Will accept only small letter alphabet from a to z

• Will terminate if 10 characters have been read / a non-vowel character is entered


sscanf()

• str contains data to be converted

• %d converts the data to integer

• num is the memory location of the converted data


String Taxonomy
Fixed-length string

• Should specify an appropriate size for the string variable

• If size is too small, will not be able to store all elements

• If size is large, memory space will be wasted unnecessarily

Variable-length string

• Better option to use

• String can be expanded or contracted to accommodate elements


Length-controlled string

• Length information stored as part of string

Variable-length string

• String ends with the delimiter ‘\0’


Operations on Strings

• String length
• Converting characters of a string into upper case
• Converting characters of a string into lower case
• Concatenation two strings to form a new string
• Appending a string to another string
• Comparing two strings
• Reverse a string
• Searching a substring
• Extracting substring from left
• Extracting substring from right
• Extracting substring from middle
• Inserting a string in another string
• Deleting a substring
Operations on Strings

• Character constant can be automatically converted into an integer value


Finding the length of a string

#include<stdio.h>
int main()
{
char str[100];
int i;
printf("Enter the string\n");
gets(str);
for(i=0;str[i]!='\0';i++);
printf("\n The length of the String %s is %d",str,i);
}
Enter the string
SASTRA

The length of the String SASTRA is 6


Finding the length of a string
i=0 1 2 3 4 5 6
S A S T R A \0 Enter the string
SASTRA
0 i=1 2 3 4 5 6
The length of the String SASTRA is 6
S A S T R A \0
0 1 i=2 3 4 5 6
S A S T R A \0
0 1 2 i=3 4 5 6
S A S T R A \0

0 1 2 3 i=4 5 6
S A S T R A \0

0 1 2 3 4 i=5 6
S A S T R A \0

0 1 2 i=3 4 5 i=6
S A S T R A \0
Converting Characters of a String into uppercase
#include<stdio.h>
int main()
{
char str[100],upper[100];
int i;
printf("Enter the string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='a'&&str[i]<='z')
{
upper[i]=str[i]-32;
}
else
{
upper[i]=str[i];
} Enter the string
} Sastra
upper[i]=‘\0’;
The Uppercase conversion is SASTRA
printf("\n The Uppercase conversion is %s",upper);
Converting Characters of a String into uppercase
str upper
i=0 1 2 3 4 5 6 i=0 1 2 3 4 5 6
S a s t r a \0 S a s t r a \0

0 i=1 2 3 4 5 6 0 i=1 2 3 4 5 6
S a s t r a \0 S A s t r a \0

0 1 i=2 3 4 5 6 0 1 i=2 3 4 5 6
S a s t r a \0 S a S t r a \0

0 1 2 i=3 4 5 6 0 1 2 i=3 4 5 6
S a s t r a \0 S a s T r a \0

0 1 2 3 i=4 5 6 0 1 2 3 i=4 5 6
S a s t r a \0 S a s t R a \0

0 1 2 3 4 i=5 6 0 1 2 3 4 i=5 6
S a s t r a \0 S a s t r A \0
Converting Characters of a String into lowercase
#include<stdio.h>
int main()
{
char str[100],lower[100];
int i;
printf("Enter the string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
lower[i]=str[i]+32;
}
else
{
lower[i]=str[i];
}
} Enter the string
lower[i]='\0'; THANJAVUR
printf("\n The lowercase conversion is %s",lower);
The lowercase conversion is thanjavur
}
Converting Characters of a String into lowercase
str upper
i=0 1 2 3 4 5 6 i=0 1 2 3 4 5 6
S a s t r a \0 S a s t r a \0

0 i=1 2 3 4 5 6 0 i=1 2 3 4 5 6
S a s t r a \0 S A s t r a \0

0 1 i=2 3 4 5 6 0 1 i=2 3 4 5 6
S a s t r a \0 S A S t r a \0

0 1 2 i=3 4 5 6 0 1 2 i=3 4 5 6
S a s t r a \0 S A S T r a \0

0 1 2 3 i=4 5 6 0 1 2 3 i=4 5 6
S a s t r a \0 S A S T R a \0

0 1 2 3 4 i=5 6 0 1 2 3 4 i=5 6
S a s t r a \0 S A S T R A \0
Concatenating two strings into a third string
#include<stdio.h>
int main()
{
char str1[100],str2[100],str3[200];
int i,j;
printf("Enter string1\n");
gets(str1);
printf("Enter string2\n");
gets(str2);
for(i=0;str1[i]!='\0';i++)
{str3[i]=str1[i]; }
for(j=0;str2[j]!='\0';i++,j++)
{str3[i]=str2[j];} Enter string1
str3[i]='\0'; SASTRA
Enter string2
printf("\n Concatenated string is %s",str3);
University
}
Concatenated string is SASTRA University
Appending a String to another string
#include<stdio.h>
int main()
{
char str1[100],str2[100];
int i,j;
printf("Enter string1\n");
gets(str1);
printf("Enter string2\n");
gets(str2);
for(i=0;str1[i]!='\0';i++);
for(j=0;str2[j]!='\0';i++,j++)
{
str1[i]=str2[j]; Enter string1
} SASTRA
Enter string2
str1[i]='\0'; University
printf("\n Appended string is %s",str1); Appended string is SASTRA University
}
String Comparison
#include<stdio.h> if(strlen(str1)!=strlen(str2))
#include<string.h> {printf("\nStrings are Not Equal");}
int main() else
{ {
char str1[100],str2[100]; for(i=0;str1[i]!='\0';i++)
int i,j; {
printf("Enter string1\n"); if(str1[i]==str2[i])
gets(str1); continue;
printf("Enter string2\n"); }
gets(str2); if(i==strlen(str2))
printf("\nStrings are Equal");
else
printf("\nStrings are Not Equal");
Enter string1 }
Mani
Enter string2 }
Mani

Strings are Equal


String Reverse
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
int i,j,len;
printf("Enter string1\n");
gets(str1);
for(len=0;str1[len]!=‘\0’;len++);
for(i=len-1,j=0;i>=0;i--,j++)
{
str2[j]=str1[i];
}
str2[j]='\0';
Enter string1
printf("Reversed String is %s",str2);
MADAME
} Reversed String is EMADAM
String Search

#include <stdio.h> for (i = 0; str[i] != '\0'; i++) {


void main() int j = 0;
{ while (str[i + j] == sub[j] && sub[j] != '\0') {
char str[100]; j++;
char sub[100]; }
int i,j,found = 0; if (sub[j] == '\0') {
printf("Enter the string\n"); printf("found at index %d",i);
gets(str); found = 1;
printf("Enter the string to be searched\n"); break;
gets(sub); }
}
if (!found) {
Enter the string printf("Substring not found.\n");
sunrise }
Enter the string to be searched
rise
}
found at index 3
Insert a substring from a text
#include<stdio.h>
for (i = len; i >= position; i--) {
#include<string.h>
str[i + wordLen] = str[i];
}
int main() {
for (i = 0; i < wordLen; i++) {
char str[200] = "Hello world!";
str[position + i] = word[i];
char word[20] = "beautiful ";
}
int len,wordLen,i,position = 6;
printf("String after insertion: %s\n", str);
printf("Original string: %s\n", str);
return 0;
len = strlen(str);
}
wordLen = strlen(word);
if (position < 0 || position > len) {
printf("Invalid position.\n");
return 1;
}
Original string: Hello world!
String after insertion: Hello beautiful world!
Delete a substring from a text

#include <stdio.h> for (i = position; i <= strLen - length; i++) {


#include <string.h> str[i] = str[i + length];
}
int main() { printf("String after deletion: %s\n", str);
char str[200] = "Hello return 0;
beautiful world!"; }
int i;
int position = 6;
int length = 10;
printf("Original string: %s\n",
str);
int strLen = strlen(str);

Original string: Hello beautiful world!


String after deletion: Hello world!
Miscellaneous string and character functions
Character Manipulation Functions
Character Manipulation Functions
String Manipulation Functions
strcat function
strncat function
strchr function
strrchr function
strcmp function
strncmp function
strcpy function
strncpy function
strlen function
strstr function
strspn function
strcspn function
strpbrk function
strtok function
strtol function
strtod function
• Accepts str that has an optional plus (+) or minus(-) sign, followed by

• Decimal number containing a sequence of decimal digits

• Optionally consisting of a decimal point

• Hexadecimal number consisting 0X or 0x followed by sequence of hexadecimal digits

• Optionally containing a decimal point

• In both cases, the number may be optionally followed by

• An exponent ‘E’ or ‘e’ for decimal constants

• ‘P’ or ‘p’ for hexadecimal constants


strtod function

You might also like