@2020 Presented By Y. N. D. Aravind 1
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton’s Group Of Institutions, Macherla
Session Objectives
Explain C Strings
Explain String Input / Output Functions
Explain Arrays of Strings
Explain String Concepts
@2020 Presented By Y. N. D. Aravind
2
Explain Strings Manipulation Functions
2
Arrays of Strings
To create an array of strings, you use a two dimensional character array, in
which the size of the left index determines the number of strings and the
size of the right index specify the maximum length of each string.
Syntax : char str[4][20];
In the above example an array of 4 strings, with each string having a
maximum length of 20 characters, those are str[0], str[1], str[2],str[3].
@2020 Presented By Y. N. D. Aravind
3
#include <stdio.h>
void main()
{
char names[10][20];
int n,i;
printf(“n How many names ”);
scanf(“%d”,&n);
flushall();
for(i=0;i<n;i++)
{
printf(“n Enter name %d ”,i);
gets(names[i]);
}
3
printf(“n The names are n”);
for(i=0;i<n;i++)
{
puts(names[i]);
}
}
Output
How many names 3
Enter name 1 NGI
Enter name 2 NIST
Enter name 3 NIE
The names are
NGI
NIST
NIE
String Functions
C does not provide any operator to deal with strings. However, C does
have a large set of useful string handling library functions. The
corresponding header file is string.h
@2020 Presented By Y. N. D. Aravind
1. strcpy( destination string, source string ): To copy source string to destination string.
2. strcat(string1, string2 ): To append string2 at end of string1(include null char )
3. strlen( string ): Returns length of the strings( no of characters )
4. strcmp( string1, string2 ): To compare two strings and returns 0(zero) if they are equal
otherwise returns a non-zero number.
5. strchr(string,char): Locate the first occurrence of a char in a string.
6. strrev( string ): Reverse of the string.
7. strncpy( string1,string2,n): To copy n chars from string2 to string1. After copy we must place a null
char at end of string1.
8. strncat( string1, string2, n): To append n chars of string2 at end of string1. After append we must
place a null char at end of string1.
9. strcmpi(string1,string2) : Same as strcmp( ), but at the time of compare it will ignore the case
sensitive.
10. strncmp( string1, string2, n): Same as strcmp( ), but it will compare first n specified chars only.
11. strncmpi( string1, string2, n): Same as strcmpi( ), but it will compare first n specified chars only.
12. strset( s1,s2): Returns the string s1 at the first occurrence of a sub string s2.
4
strcpy( )
This function is used to copy one string to the other. Its syntax is as follows:
strcpy(string1,string2);
where string1 and string2 are one-dimensional character arrays.
This function copies the content of string2 to string1.
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main ()
{
char string1[30],string2[30];
printf(“n Enter first string : ”);
gets(string1);
printf(“n Enter second string:”);
gets(string2);
strcpy(string1,string2);
printf(“n First string = %s”,string1);
printf(“n Second string =
%s”,string2);
}
5
OUTPUT
Enter first string : master
Enter second string : madam
First string = madam
Second string = madam
strcat ( )
This function is used to concatenate two strings. i.e., it appends one string at the end of the
specified string. Its syntax as follows :
strcat(string1,string2);
where string1 and string2 are one-dimensional character arrays.
This function joins two strings together. In other words, it adds the string2 to string1 and the
string1 contains the final concatenated string. E.g., string1 contains prog and string2 contains ram,
then string1 holds program after execution of the strcat() function.
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main()
{
char string1[30],string2[30];
printf(“n Enter first string : ”);
gets(string1);
printf(“n Enter second string:”);
gets(string2);
strcat(string1,string2);
printf(“n First string = %s”,string1);
printf(“n Second string = %s”,string2);
}
6
OUTPUT
Enter first string : prog
Enter second string : ram
First string = program
Second string = ram
strncat( )
In the previous slide we discussed strcat() function, which is used for concatenation of one string
to another string. In this guide, we will see a similar function strncat(), which is same as strcat()
except that strncat() appends only the specified number of characters to the destination string.
Syntax:- char *strncat(char *str1, const char *str2, size_t n);
str1 – Destination string.
str2 – Source string which is appended at the end of destination string str1.
n – number of characters of source string str2 that needs to be appended.
@2020 Presented By Y. N. D. Aravind 7
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[50], str2[50];
//destination string strcpy(str1, "This is my initial string");
//source string strcpy(str2, ", add this");
//displaying destination string printf("String after concatenation: %sn", strncat(str1, str2, 5));
// this should be same as return value of strncat() printf("Destination String str1: %s", str1);
return 0;
} OUTPUT
String after concatenation: This is my initial string, add
Destination String str1: This is my initial string, add
strlen( )
This function is used to find the length of the string excluding the NULL character. In other words,
this function is used to count the number of characters in a string. Its syntax is as follows:
int strlen(string);
Example: char str1[ ] = “WELCOME”;
int n;
n = strlen(str1);
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main()
{
char string1[50];
int length;
printf(“n Enter any string : ”);
gets(string1);
length=strlen(string1);
printf(“n The length of string = %d”,length);
}
8
OUTPUT
Enter any string : WELCOME
The length of string = 7
strcmp ( )
This function compares two strings character by character (ASCII comparison) and returns one of
three values {-1,0,1}. The numeric difference is „0‟ if strings are equal .If it is negative string1 is
alphabetically above string2 .If it is positive string2 is alphabetically above string1.
Its syntax is as follows : int strcmp(string1,string2);
Example : char str1[ ] = “ROM”;
char str2[ ] =”RAM”;
strcmp(str1,str2); (or) strcmp(“ROM”,”RAM”);
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main()
{
char string1[30],string2[15];
int x;
printf(“n Enter first string:”);
gets(string1);
printf(“n Enter second string:”);
gets(string2);
x=strcmp(string1,string2);
if(x==0)
printf(“n Both strings are equal”);
else if(x>0)
printf(“n First string is bigger”);
else
printf(“n Second string is bigger”);
}
9
OUTPUT
Enter first string : ROM
Enter second string : RAM
First string is bigger
strncmp ( )
In the last tutorial we discussed strcmp() function which is used for comparing
two strings. In this guide, we will discuss strncmp() function which is same as
strcmp(), except that strncmp() comparison is limited to the number of
characters specified during the function call. For example strncmp(str1, str2, 4)
would compare only the first four characters of strings str1 and str2.
Syntax:-
int strncmp(const char *str1, const char *str2, size_t n)
str1 – First String
str2 – Second String
n – number of characters that needs to be compared.
Return value of strncmp()
This function compares only the first n (specified number of) characters of
strings and returns following value based on the comparison.
0, if both the strings str1 and str2 are equal
>0, if the ASCII value of first unmatched character of str1 is greater than str2
<0, if the ASCII value of first unmatched character of str1 is less than str2
@2020 Presented By Y. N. D. Aravind 10
strrev ( )
The function can be used to reverse a string.
Syntax:- strrev(str);
@2020 Presented By Y. N. D. Aravind
#include<stdio.h>
#include<conio.h>
Void main
{
char str1[10];
printf(“n enter string”);
gets(str);
strrev(str);
puts(“The reverse string of a given string is ”);
puts(str);
}
11
OUTPUT
Enter string : COLLEGE
The reverse string of a given string is EGELLOC
strchar( )
The function strchr() searches the occurrence of a specified character in the given string and
returns the pointer to it.
Syntax :-
char *strchr(const char *str, int ch)str – The string in which the character is searched.
ch – The character that is searched in the string str.
Return Value of strchr()
It returns the pointer to the first occurrence of the character in the given string, which means that if
we display the string value of the pointer then it should display the part of the input string starting
from the first occurrence of the specified character.
@2020 Presented By Y. N. D. Aravind
#include <stdio.h>
#include <string.h>
int main ()
{
const char str[] = "This is just a String";
const char ch = 'u';
char *p;
p = strchr(str, ch);
printf("String starting from %c is: %s", ch, p);
return 0;
}
12
Output
String starting from u is: ust a String
Write a program to print whether the string is palindrome or not.
@2020 Presented By Y. N. D. Aravind
#include <stdio.h>
#include <string.h>
int main ()
{
char str[10],str1[10];
int x;
printf(“n Enter string ”);
gets(str);
strcpy(str1,str)
strrev(str);
x=strcmp(str,str1);
if(x==0)
{
printf(“n %s is palindrome”,str);
}
else
{
printf(“n %s is not palindrome”,str);
}
}
13
Output -1
Enter string liril
liril is palindrome
Output -2
Enter string newton
newton is not palindrome
Thank You
@2020 Presented By Y. N. D. Aravind
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton’s Group Of Institutions, Macherla
14

Strings part2

  • 1.
    @2020 Presented ByY. N. D. Aravind 1 Presented By Y. N. D. ARAVIND M.Tech, Dept of CSE Newton’s Group Of Institutions, Macherla
  • 2.
    Session Objectives Explain CStrings Explain String Input / Output Functions Explain Arrays of Strings Explain String Concepts @2020 Presented By Y. N. D. Aravind 2 Explain Strings Manipulation Functions 2
  • 3.
    Arrays of Strings Tocreate an array of strings, you use a two dimensional character array, in which the size of the left index determines the number of strings and the size of the right index specify the maximum length of each string. Syntax : char str[4][20]; In the above example an array of 4 strings, with each string having a maximum length of 20 characters, those are str[0], str[1], str[2],str[3]. @2020 Presented By Y. N. D. Aravind 3 #include <stdio.h> void main() { char names[10][20]; int n,i; printf(“n How many names ”); scanf(“%d”,&n); flushall(); for(i=0;i<n;i++) { printf(“n Enter name %d ”,i); gets(names[i]); } 3 printf(“n The names are n”); for(i=0;i<n;i++) { puts(names[i]); } } Output How many names 3 Enter name 1 NGI Enter name 2 NIST Enter name 3 NIE The names are NGI NIST NIE
  • 4.
    String Functions C doesnot provide any operator to deal with strings. However, C does have a large set of useful string handling library functions. The corresponding header file is string.h @2020 Presented By Y. N. D. Aravind 1. strcpy( destination string, source string ): To copy source string to destination string. 2. strcat(string1, string2 ): To append string2 at end of string1(include null char ) 3. strlen( string ): Returns length of the strings( no of characters ) 4. strcmp( string1, string2 ): To compare two strings and returns 0(zero) if they are equal otherwise returns a non-zero number. 5. strchr(string,char): Locate the first occurrence of a char in a string. 6. strrev( string ): Reverse of the string. 7. strncpy( string1,string2,n): To copy n chars from string2 to string1. After copy we must place a null char at end of string1. 8. strncat( string1, string2, n): To append n chars of string2 at end of string1. After append we must place a null char at end of string1. 9. strcmpi(string1,string2) : Same as strcmp( ), but at the time of compare it will ignore the case sensitive. 10. strncmp( string1, string2, n): Same as strcmp( ), but it will compare first n specified chars only. 11. strncmpi( string1, string2, n): Same as strcmpi( ), but it will compare first n specified chars only. 12. strset( s1,s2): Returns the string s1 at the first occurrence of a sub string s2. 4
  • 5.
    strcpy( ) This functionis used to copy one string to the other. Its syntax is as follows: strcpy(string1,string2); where string1 and string2 are one-dimensional character arrays. This function copies the content of string2 to string1. @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main () { char string1[30],string2[30]; printf(“n Enter first string : ”); gets(string1); printf(“n Enter second string:”); gets(string2); strcpy(string1,string2); printf(“n First string = %s”,string1); printf(“n Second string = %s”,string2); } 5 OUTPUT Enter first string : master Enter second string : madam First string = madam Second string = madam
  • 6.
    strcat ( ) Thisfunction is used to concatenate two strings. i.e., it appends one string at the end of the specified string. Its syntax as follows : strcat(string1,string2); where string1 and string2 are one-dimensional character arrays. This function joins two strings together. In other words, it adds the string2 to string1 and the string1 contains the final concatenated string. E.g., string1 contains prog and string2 contains ram, then string1 holds program after execution of the strcat() function. @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main() { char string1[30],string2[30]; printf(“n Enter first string : ”); gets(string1); printf(“n Enter second string:”); gets(string2); strcat(string1,string2); printf(“n First string = %s”,string1); printf(“n Second string = %s”,string2); } 6 OUTPUT Enter first string : prog Enter second string : ram First string = program Second string = ram
  • 7.
    strncat( ) In theprevious slide we discussed strcat() function, which is used for concatenation of one string to another string. In this guide, we will see a similar function strncat(), which is same as strcat() except that strncat() appends only the specified number of characters to the destination string. Syntax:- char *strncat(char *str1, const char *str2, size_t n); str1 – Destination string. str2 – Source string which is appended at the end of destination string str1. n – number of characters of source string str2 that needs to be appended. @2020 Presented By Y. N. D. Aravind 7 #include <stdio.h> #include <string.h> int main () { char str1[50], str2[50]; //destination string strcpy(str1, "This is my initial string"); //source string strcpy(str2, ", add this"); //displaying destination string printf("String after concatenation: %sn", strncat(str1, str2, 5)); // this should be same as return value of strncat() printf("Destination String str1: %s", str1); return 0; } OUTPUT String after concatenation: This is my initial string, add Destination String str1: This is my initial string, add
  • 8.
    strlen( ) This functionis used to find the length of the string excluding the NULL character. In other words, this function is used to count the number of characters in a string. Its syntax is as follows: int strlen(string); Example: char str1[ ] = “WELCOME”; int n; n = strlen(str1); @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main() { char string1[50]; int length; printf(“n Enter any string : ”); gets(string1); length=strlen(string1); printf(“n The length of string = %d”,length); } 8 OUTPUT Enter any string : WELCOME The length of string = 7
  • 9.
    strcmp ( ) Thisfunction compares two strings character by character (ASCII comparison) and returns one of three values {-1,0,1}. The numeric difference is „0‟ if strings are equal .If it is negative string1 is alphabetically above string2 .If it is positive string2 is alphabetically above string1. Its syntax is as follows : int strcmp(string1,string2); Example : char str1[ ] = “ROM”; char str2[ ] =”RAM”; strcmp(str1,str2); (or) strcmp(“ROM”,”RAM”); @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main() { char string1[30],string2[15]; int x; printf(“n Enter first string:”); gets(string1); printf(“n Enter second string:”); gets(string2); x=strcmp(string1,string2); if(x==0) printf(“n Both strings are equal”); else if(x>0) printf(“n First string is bigger”); else printf(“n Second string is bigger”); } 9 OUTPUT Enter first string : ROM Enter second string : RAM First string is bigger
  • 10.
    strncmp ( ) Inthe last tutorial we discussed strcmp() function which is used for comparing two strings. In this guide, we will discuss strncmp() function which is same as strcmp(), except that strncmp() comparison is limited to the number of characters specified during the function call. For example strncmp(str1, str2, 4) would compare only the first four characters of strings str1 and str2. Syntax:- int strncmp(const char *str1, const char *str2, size_t n) str1 – First String str2 – Second String n – number of characters that needs to be compared. Return value of strncmp() This function compares only the first n (specified number of) characters of strings and returns following value based on the comparison. 0, if both the strings str1 and str2 are equal >0, if the ASCII value of first unmatched character of str1 is greater than str2 <0, if the ASCII value of first unmatched character of str1 is less than str2 @2020 Presented By Y. N. D. Aravind 10
  • 11.
    strrev ( ) Thefunction can be used to reverse a string. Syntax:- strrev(str); @2020 Presented By Y. N. D. Aravind #include<stdio.h> #include<conio.h> Void main { char str1[10]; printf(“n enter string”); gets(str); strrev(str); puts(“The reverse string of a given string is ”); puts(str); } 11 OUTPUT Enter string : COLLEGE The reverse string of a given string is EGELLOC
  • 12.
    strchar( ) The functionstrchr() searches the occurrence of a specified character in the given string and returns the pointer to it. Syntax :- char *strchr(const char *str, int ch)str – The string in which the character is searched. ch – The character that is searched in the string str. Return Value of strchr() It returns the pointer to the first occurrence of the character in the given string, which means that if we display the string value of the pointer then it should display the part of the input string starting from the first occurrence of the specified character. @2020 Presented By Y. N. D. Aravind #include <stdio.h> #include <string.h> int main () { const char str[] = "This is just a String"; const char ch = 'u'; char *p; p = strchr(str, ch); printf("String starting from %c is: %s", ch, p); return 0; } 12 Output String starting from u is: ust a String
  • 13.
    Write a programto print whether the string is palindrome or not. @2020 Presented By Y. N. D. Aravind #include <stdio.h> #include <string.h> int main () { char str[10],str1[10]; int x; printf(“n Enter string ”); gets(str); strcpy(str1,str) strrev(str); x=strcmp(str,str1); if(x==0) { printf(“n %s is palindrome”,str); } else { printf(“n %s is not palindrome”,str); } } 13 Output -1 Enter string liril liril is palindrome Output -2 Enter string newton newton is not palindrome
  • 14.
    Thank You @2020 PresentedBy Y. N. D. Aravind Presented By Y. N. D. ARAVIND M.Tech, Dept of CSE Newton’s Group Of Institutions, Macherla 14