Professional Documents
Culture Documents
CTSD Workshop Content
CTSD Workshop Content
CTSD Workshop Content
Revision of
Concepts
INTRODUCTION TO C LANGUAGE
lNTRODUCTlON
Functions Description
Strlen() Determines the length of a string
Strcpy() Copies astring from source to destination
Strncpy() Copies charcters of a string to another string upto the specified
length
Stricmp() Compares characters of two strings
Strcmp() Compares characters of two strings upto the specified length
Strncmp() Compares characters of two strings upto the specified length
Strnicmp() Compares characters of two strings upto the specified length
Strlwr() Converts uppercase characters of a string to lower case
Strupr() Converts lowercase characters of a string to upper case
Strdup() Duplicates a string
STANDARD ЅTRINGЅ FUNTIONS
Call by value
In this type, value of actual arguments is passed to the formal arguments and the operation is
done on the formal arguments. Any change made in the formal argument does not effect the actual
arguments because formal arguments are photo copies of actual arguments.
Call by reference
In this type, instead of passing values, addresses are passed. Function operates on address rather
than values. Here the formal arguments are pointers to the actual argument.
Pointers
Hands On
Exercise
1. Program to find sum of two numbers.
#include<stdio.h>
#include<conio.h> void main()
{
int a,b,s;
clrscr();
printf(“Enter two no: ”);
scanf(“%d%d",&a,&b); s=a+b;
printf(“sum=%d”,s); getch();
}
Output:
Enter two no: 5 6
sum=11
1. Program to find the simple interest.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,r,t,si;
clrscr();
printf(“enter principle, Rate of interest & time to find simple interest: ”);
scanf(“%d%d%d”,&p,&r,&t);
si=(p*r*t)/100;
printf(“simple intrest= %d”,si); getch();
}
Output:
enter principle, rate of interest & time to find simple interest: 500 5
2
simple interest=50
1. Program to calculate sum of 5 subjects and find percentage.
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf(“enter marks of 5 subjects: ”);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5)
; sum=s1+s2+s3+s4+s5;
printf(“sum=%d”,sum);
per=(sum*100)/total;
printf(“percentage=%f”,per);
getch();
}
Program to reverse a given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,r=0; clrscr();
printf(“enter any no to get its reverse: ”);
scanf(“%d”,&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
}
printf(“reverse=%d”,r); getch();
Program to show the use of conditional operator.
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“enter value for a & b: ”);
scanf(“%d%d”,&a,&b);
(a>b)?printf(“a is greater”):printf(“b is reater”);
getch();
}
Program to find whether given no. is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“enter any no: ”);
scanf(“%d”,&n);
if(n%2==0)
printf(“no is even”);
else
printf(“no is odd”);
getch();
}
8. Program to use switch statement. Display Monday to Sunday.
void main()
{
char ch;
printf(“enter m for Monday\nt for Tuesday\nw for Wednesday\nh for Thursday\nf for Friday\ns for Saturday\nu for Sunday);
scanf(“%c”,&ch); switch(ch)
{
case ‘m’:
case ‘M’: printf(“monday”); break;
case ‘t’:
case ‘T’: printf(“tuesday”); break;
case ‘w’:
case ‘W’: printf(“wednesday”); break;
case ‘h’:
case ‘H’: printf(“thursday”); break;
case ‘f ’:
case ‘F’: printf(“friday”); break;
case ‘s’:
case ‘S’: printf(“saturday”); break;
case ‘u’:
case ‘U’: printf(“sunday”); break;
default : printf(“wrong input”); break;
}
Program to display arithmetic operator using switch case.
void main()
{
int a,b,n,s,m,su,d;
printf(“enter two no’s : ”);
scanf(“%d%d”,&a,&b);
printf(“enter 1 for sum\n2 for multiply\n3for subtraction\n4 for division: ”); scanf(“%d”,&n);
switch(n)
{
case 1:
s=a+b; printf(“sum=%d”,s); break;
case 2:
m=a*b; printf(“multiply=%d”,m); break;
case 3:
su=a-b; printf(“subtraction=%d”,su); break;
case 4:
d=a/b; printf(“divission=%d”,d); break;
default: printf(“wrong input”); break;
}
getch();
Program to add two number using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p1,*p2,sum;
printf(“enter two no’s: ”);
scanf(“%d%d”,&*p1,&*p2);
sum=*p1+*p2;
printf(“sum=%d”,sum);
getch();
}
Output:
enter two no’s: 10 20
sum=30
Program to find the maximum number in array using pointer.
void main()
{
int max,i,*a[5];
clrscr();
printf(“enter element for the array: ”);
for(i=0;i<5;i++)
scanf(“%d”,&*a[i]);
max=*a[0]; for(i=1;i<5;i++)
{
if(max<*a[i])
max=*a[i];
}
printf(“maximum no= %d”,max);
getch();
Program to find square of a number using functions.
#include<stdio.h>
#include<conio.h>
void main()
{
int rev(int);
int r,a; clrscr();
printf(“enter any no: ”);
scanf(“%d”,&a); r=rev(a);
printf(“square is : %d”,r);
getch();
}
int rev(int x)
{
return(x*x);
}
Program to swap two numbers using functions.
void swap(int a,int b)
#include<stdio.h>
{
#include<conio.h> int
void main() temp;
{
temp=a;
void swap(int,int);
a=b;
int a,b,r;
b=temp;
clrscr();
printf(“after swapping the value for a & b is : %d %d”,a,b);
printf(“enter value for a&b:
}
”);
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
}
Program to show call by value.
int swap(int x,int y)
void main()
{
{
int temp;
int a,b,swap();
temp=x;
a=5; b=10;
x=y;
printf(”value of a=%d & value of b=%d before
y=temp;
swap ”,a,b); swap(a,b);
}
printf(“\nvalue of a =%d & b=%d after
swap”,a,b);
}
Program to show call by reference.
#include<stdio.h> int swap(int *x,int *y)
#include<conio.h> {
int
void main()
temp;
{
int a,b,*aa,*bb,swap(); temp=*
x;
a=5;
*x=*y;
b=10;
*y=temp;
aa=&a;
}
bb=&b;
printf(“value of a= %d & value of b=%d before swap”,a,b);
swap(aa,bb);
printf(“\nvalue of a=%d & b=%d after swap”,a,b);}
Write a c program to calculate Area of Rectangle , Perimeter of a
// calculate area
Rectangle and Diagonal of a Rectangle.
a = l * b;
#include <stdio.h>
// calculate perimeter
#include <math.h>
p = 2 * (l + b);
int main() {
// calculate diagonal
// declare variables
d = sqrt(l * l + b * b);
float l, b, a, p, d;
// display result upto 3 decimal places
// take input
printf("Area: %0.3f\n", a);
printf("Enter the length of rectangle: ");
printf("Perimeter: %0.3f\n", p);
scanf("%f", & l);
printf("Diagonal: %0.3f\n", d);
printf("Enter the breadth of rectangle: ");
return 0;
scanf("%f", & b);
}
To insert a sub-string into a given main string from a given position.
#include<stdio.h> printf("Enter the position where the string is to be
#include<conio.h> inserted\n");
#include<string.h> scanf("%d", &n);
void main() for(i = n; i < l1; i++)
{ {
char str1[20], str2[20]; str1[i + l2] = str1[i];
int l1, l2, n, i; }
clrscr(); for(i = 0; i < l2; i++)
puts("Enter the string 1\n"); {
gets(str1); str1[n + i] = str2[i];
l1 = strlen(str1); }
puts("Enter the string 2\n"); str2[l2 + 1] = '\0';
gets(str2); printf("After inserting the string is %s", str1);
l2 = strlen(str2); getch();
}
To delete n Characters from a given position in a given string,
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
for(i = pos + n; i < l; i++)
{
{
char str[20];
str[i - n] = str[i];
int i, n, l, pos;
}
clrscr();
str[i - n] = '\0';
puts("Enter the string\n");
printf("The string is %s", str);
gets(str);
getch();
printf("Enter the position where the characters are to be deleted\n");
}
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
1. Write a C program to determine if the given string is a palindrome or not.
#include <stdio.h> int main()
#include <string.h> {
int checkpalindrome(char *s)
{ char s[1000];
int i,c=0,n;
n=strlen(s); printf("Enter the string: ");
for(i=0;i<n/2;i++) gets(s);
{
if(s[i]==s[n-i-1])
c++; if(checkpalindrome(s))
} printf("string is palindrome");
if(c==i) else
return 1; printf("string is not palindrome");
else
return 0; }
}
Write a C program that displays the position or index in the
string S where the string T begins, or -1 if S does not contain T.
void main() if(found)
{ {
char s[30], t[20]; printf("Second String is found in the First String at
char *found; %d position.\n", found - s);
clrscr(); }
puts("Enter the first string: "); else
gets(s); {
puts("Enter the string to be searched: "); printf("-1");
gets(t); }
found = strstr(s, t); }
1. Write a C program to generate Pascal's triangle.
#include <stdio.h>
void printPascal(int n)
{ int main()
for (int line = 1; line <= n; line++) { {
for (int space = 1; space <= n - line; space++) int n = 5;
printf(" "); printPascal(n);
// used to represent C(line, i) return 0;
int coef = 1; }
for (int i = 1; i <= line; i++) {
// The first value in a line
// is always 1
printf("%4d", coef);
coef = coef * (line - i) / i;
}
printf("\n");
}
}
1. Write a C program to construct a pyramid of numbers.
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Any
Doubt??
www.paruluniversity.ac.in