You are on page 1of 27

String

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);

Syntax: strcat(destination,source); strcat(s,s1);

🠶 Program: puts(“The concatenated string is:”);

#include<stdio.h> puts(s);

#include<conio.h> getch();

#include<string.h> }

void main() Output:

{ Enter the first string: come

char s[10],s1[20]; Enter the second string: in

clrscr(); The concatenated string is:

puts("Enter the first string:"); comein

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<conio.h> #include<string.h> void main()


{
char s[10],s1[10];
clrscr();
puts("Enter a string");
gets(s);
strcpy(s1,s); strrev(s);
if(strcmp(s,s1)==0)
puts("Sring is a palindrome");
else
puts("String is not a palindrome");
getch();
}
Write a C program to find the sum of digits of a number
#include<stdio.h>  
 int main()    
{    
int n,sum=0,m;    
printf("Enter a number:");    
scanf("%d",&n);    
while(n>0)    
{    
m=n%10;    
sum=sum+m;    
n=n/10;    
}    
printf("Sum is=%d",sum);    
return 0;  
}   
Write a C program to print the Fibonacci series of a given number.

#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

🠶 This is also called as exit controlled loop.


🠶 Syntax:  
do
{
Single statement or
Block of statements;
}while(conditional expression);
Program
#include <stdio.h>
void main()
{
int i=0;
do
{
printf("%d\t",i);
i++;
} while(i<10);
}
Output : 0 1 2 3 4 5 6 7 8 9
Managing Input and Output Statements
Definition

🠶 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

🠶 The I/O functions are classified into two types:


1. Formatted I/O functions
2. Unformatted I/O Functions
Formatted I/O Functions

🠶 Formatted I/O function accepts or present in a particular format.


🠶 printf(): Used to print any combination of data.
🠶 Syntax:
🠶 printf(―control_string 1, - - , control_string n‖,variable1, - - - , variable n);
🠶 Control String may be format specifier, escape sequence, string to be displayed.
🠶 Format specifier Data type
🠶 %d Integer
🠶 %f Float
🠶 %c Character
🠶 %s string
Scanf
scanf(): reads all types of data values. It is used for runtime assignment of variables.
Syntax:
scanf(―control_string 1, - - , control_string n‖,&variable1, - - - ,variable n);
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int no; char ch; char s[20];
 printf("Enter a character : ");
scanf("%c",&ch); printf("Enter a No : ");
scanf("%d",&no);
printf("Enter a Word or String : ");
scanf("%s",&s);
printf("\n Character : %c",ch);
printf("\n No : %d",no); printf("\n String : %s",s); getch();
}
Unformatted I/O functions

🠶 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

🠶 These are the functions that are used to input/output of whole


string.
1. gets():reads a whole line of input into a string until a newline or EOF is encountered. It is
critical to ensure that the string is large enough to hold any expected input lines. When all input
is finished, NULL as defined in stdio.h is returned.

🠶 Syntax: gets(str var); Ex: gets(s);


2. puts(): writes a string to the output means it displays the character on the screen, and ends it
with a newline character.

🠶 Syntax: puts(string); Ex : puts(hai);


Program
#include<stdio.h>
#include<conio.h>
void main()
{
char s[20];
clrscr();
puts("Enter a string");
gets(s);
puts("The string is");
puts(s);
getch();
}
Output: Enter a string:
Hai
The string is Hai

You might also like