You are on page 1of 21

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• String Handling in C

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


String or Array of character
String is a sequence of characters that is treated as a single data item and terminated by
null character '\0'. Remember that C language does not support strings as a data type.
A string is actually one-dimensional array of characters in C language. These are often
used to create meaningful and readable programs.

 Strings are used in string handling operations such as,


Counting the length of a string.
Comparing two strings.
Copying one string to another.
Converting lower case string to upper case.
Converting upper case string to lower case.
Joining two strings.
Reversing string.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


String or Array of character

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


String or Array of character

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Memory Representation of String

0 1 2 3 4 5 6 7 8
h y d r o g e n \0

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Input & Output of String

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


gets() & puts() function in String
 The gets() and puts() functions facilitate the transfer of strings between the
computer and the standard input/output devices.
 Each of these functions accepts a single argument.
 The argument must be a data item that represents a string (e.g. character
array).
 The string may include white space characters.
 In the case of gets, the string will enter from the keyboard and will terminate
with a new line character (i.e. the string will end when the user presses the
RETURN key).
 The gets and puts functions offer simple alternatives to the use of scanf()
and printf() for reading and displaying strings:

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of gets() & puts() function

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Find the length of the String
#include <stdio.h>
void main()
{
char s[1000], i;
printf("Enter a string: ");
gets(s);
for(i=0; s[i]!='\0'; ++i);
printf("Length of string: %d",i);
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Find the frequency of character in a String
#include <stdio.h>
void main()
{
char c[50],ch;
int i,count=0;
printf("Enter a string: ");
gets(c);
printf("Enter a character to find frequency: ");
scanf("%c",&ch);
for(i=0;c[i]!='\0';++i)
{
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
} http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Concatenate two String
#include <stdio.h>
void main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
gets(s1);
printf("Enter second string: ");
gets(s2);
for(i=0; s1[i]!='\0'; ++i); /* i contains length of string s1. */
for(j=0; s2[j]!='\0'; ++j, ++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
printf("After concatenation: %s",s1);
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Reverse a String
for (i = len - 1; i >= 0; i--)
{
revstr[j++] = str[i];
}
revstr[i] = '\0';
printf("\n String after Reversing = %s", revstr);
printf(“\n Original string is=%s” ,str);
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1) What is a String in C Language.?


A) String is a new Data Type in C
B) String is an array of Characters with null character as the last element of array.
C) String is an array of Characters with null character as the first element of array
D) String is an array of Integers with 0 as the last element of array. Answer B
 
2) Choose a correct statement about C String.
char ary[]="Hello..!";
A) Character array, ary is a string. B) ary has no Null character at the end
C) String size is not mentioned D) String can not contain special characters. Answer A

3) What is the Format specifier used to print a String or Character array in C printf or scanf function?
A) %c B) %C
C) %s D) %w Answer C

4) What is the maximum length of a C String?


A) 32 characters B) 64 characters
C) 256 characters D) None of the above Answer D
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

5) What is the output of C Program with Strings?


int main()
{
char ary[]="Discovery Channel";
printf("%s",ary);
return 0;
}
A) D B) Discovery Channel
C) Discovery D) Compiler error Answer B

6) What is the output of C Program with Strings.?


int main()
{
char str[]={'g','l','o','b','e'};
printf("%s",str);
return 0;
}
A) G B) globe
C) globe\0 D) None of the above Answer D

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

7) What is the output of C Program with Strings.?


int main()
{
char str[]={'g','l','o','b','y','\0'};
printf("%s",str);
return 0;
}
A) g B) globe
C) globe\0 D) Compiler error Answer B
 
8) How do you convert this char array to string.?
char str[]={'g','l','o','b','y'};
A) str[5] = 0; B) str[5] = '\0'
C) str[]={'g','l','o','b','y','\0'}; D) All the above Answer D

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

9) What is the output of C Program?


int main()
{
int str[]={'g','l','o','b','y'};
printf("A%c ",str);
printf("A%s ",str);
printf("A%c ",str[0]);
return 0;
}
A) A A A B) A Ag Ag
C) A*randomchar* Ag Ag D) Compiler error Answer C
10) What is the output of C Program with arrays?
int main()
{
char str[]={"C","A","T","\0"};
printf("%s",str);
return 0;
}
A) C B) CAT
C) CAT\0 D) Compiler error Answer D
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Assignments
 Write a program to input any string and print the string.
 Write a program to find the Length of a string
 Write a program to input any string and print it reverse order
 Check any input string is palindrome or not.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like