Professional Documents
Culture Documents
String - Definition
In C programming, array of character are called strings. A string is terminated by null character \0.
“c string tutorial”
Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of
string.
c S t R i n G t u T o r I a l \0
String
Declaration of strings: Strings are declared in C in similar manner as arrays. Only difference is that, strings are of
char type.
char s[5];
Initialization of strings:
char c[]="abcd"; OR char c[5]="abcd"; OR char c[]={'a','b','c','d','\0'};OR
char c[5]={'a','b','c','d','\0'};
String
Program:
#include <stdio.h>
int main(){
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s.",name);
return 0;
}
Output:
Enter name: Dennis Ritchie
Your name is Dennis.
String operations
strlen(): In C, strlen() function calculates the length of string. It is defined under "string.h" header file. It takes only
one argument, i.e, string name.Function strlen() returns the value of type integer.
Syntax: variablename = strlen(string_name);
String operations
Program:
#include <stdio.h>
#include <string.h>
void main()
{
char c[20];
printf("Enter string: ");
gets(c);
printf("Length of string c=%d \n",strlen(c));
}
Output : Enter string: String
Length of string c=6
String operations
strcpy():Function strcpy() copies the content puts("Enter a string");
of one string to the content of another string. gets(s1);
It is defined under "string.h" header file. It
takes two arguments. strcpy(s,s1);
Syntax: strcpy(destination,source); puts("The string after copy");
Program: puts(s);
#include<stdio.h> getch();
#include<conio.h> }
#include<string.h> Output:
void main() Enter a string:
{ Hai
char s[10],s1[10]; The string after copy
clrscr(); Hai
String operations
strcat():In C programming, strcat() concatenates(joins) two puts("Enter the second string:");
strings.It takes two arguments, i.e, two strings and resultant
string is stored in the first string specified in the argument. gets(s1);
#include<stdio.h> puts(s);
#include<conio.h> getch();
#include<string.h> }
gets(s);
String operations
strcmp(): In C, strcmp() compares two string and returns gets(b);
value 0, if the two strings are equal. It is defined under
if( strcmp(a,b) == 0 )
"string.h" header file.
printf("Entered strings are equal.\n");
Syntax: temp_varaible=strcmp(string1,string2);
else
Program:
printf("Entered strings are not equal.\n");
#include <stdio.h>
getch();
#include <string.h>
}
void main()
Output:
{
Enter the first string:hai
char a[100], b[100];
Enter the second string: Hai
clrscr();
Entered strings are not equal.
printf("Enter the first string:");
gets(a);
printf("Enter the second string:");
String operations
strrev(): This function reverses a given string in C language.
Syntax: strrev(str)
Program:
#include <stdio.h>
#include <string.h>
void main()
{
char arr[100];
printf("Enter a string to reverse\n");
gets(arr);
strrev(arr);
printf("Reverse of entered string is %s",arr);
}
Output:
Enter a string: welcome
Reverse of entered sting is emoclew
String operations
strlwr() : function converts all the uppercase characters in that string to lowercase characters. The resultant from
strlwr() is stored in the same string.
Syntax: strlwr(string_name);
Program:
#include <stdio.h>
#include <string.h>
void main()
{
char str1[]="LOWer Case";
puts(strlwr(str1)); //converts to lowercase and displays it.
}
Output: lower case
String operations
strupr(): function converts all the lowercase characters in that string to uppercase characters. The resultant from
strupr() is stored in the same string.
Syntax: strlwr(string_name);
Program:
#include <stdio.h>
#include <string.h>
void main()
{
char str1[]="UPPEer case";
puts(strupr(str1)); //converts to upper case and displays it.
}
Output: UPPER CASE
Write a C program to count the number of vowels in a string.
#include<stdio.h> #include<conio.h> void main()
{
char ch[10]; int i,count=0; clrscr();
printf("Enter the string\n");
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
if(ch[i]=='a' || ch[i]=='e' || ch[i]=='i' || ch[i]=='o' || ch[i]=='u')
{
count++;
}}
printf(“No of vowels - %d”,count); getch();
}
Write a C program to check whether a String is palindrome or not.
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
do ... while loop
🠶 Input : In any programming language input means to feed some data into program. This
can be given in the form of file or from command line. C programming language provides
a set of built-in functions to read given input and feed it to the program as per
requirement.
🠶 Output : In any programming language output means to display some data on screen,
printer or in any file. C programming language provides a set of built- in functions to
output required data.
Types of I/O
🠶 This function cannot control the format of reading and writing the data.
🠶 Types
1. Character I/O
2. String I/O
Character I/O
🠶 These are the functions that can input/output of one character at a time. These functions deal
with the individual character value. Some of the character I/O functions are:
1. getchar():This is used for reading a character from the keyboard.
🠶 Syntax: Char_variable = getchar(); Ex.:- char ch; ch=getchar( );
🠶 This statement accepts a character and stores it to "ch". In this function the typed character is
echoed to the monitor and waits for carriage return (Enter) to assign it to variable.
2. putchar(): It is used to display a character Syntax: putchar(character_variable/'character') Ex:
putchar(ch);
3. getch():It also accepts a single character. It doesn't display the character at the time of typing.
And also it doesn't require enter to assign value to variable.
🠶 Syntax: Char_variable = getch()
4. putch(): It is also used to display a single character on the screen.
🠶 Syntax : putch(variable name);
5. getche():It is same as getch( ) but only difference is it it echoes the character at the time of
typing on the monitor
Example
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character:");
ch=getchar();
putchar(ch);
getch();
}
Output: Enter a character: a
a
String I/O Functions