Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Basic Programming Lab

The document provides code snippets and explanations for basic C programming concepts related to arrays and strings, including declaring and initializing 1D arrays, searching elements in an array, declaring character arrays, formatting output in printf, and common string and character functions.

Uploaded by

IsSid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Basic Programming Lab

The document provides code snippets and explanations for basic C programming concepts related to arrays and strings, including declaring and initializing 1D arrays, searching elements in an array, declaring character arrays, formatting output in printf, and common string and character functions.

Uploaded by

IsSid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Basic Programming Lab

— — 0x08
1D-Array
// Program to declare and initialise 1D Array

#include <stdio.h>
int main()
{
int arr[10];

for (int i = 0; i < 10; ++i)


{
printf("\nEnter element no%d: ", i +
1);
scanf("%d", &arr[i]);
}
for (int i = 0; i < 10; ++i)
{
printf("\n%d", arr[i]);
}
return 0;
}

/*
Another type of declaration
int arr[] = {1,2,3,4,5}; //array of size 5
*/
// Program to search an element in an array

#include <stdio.h>
int main()
{
int arr[10], x, flag = 0;

for (int i = 0; i < 10; ++i)


{
printf("\nEnter element no%d: ", i +
1);
scanf("%d", &arr[i]);
}

printf("\nEnter the no u want to search: ");


scanf("%d", &x);
for (int i = 0; i < 10; ++i)
{
if (arr[i] == x)
{
flag = 1;
printf("\nSearch Successful,
found the no %d", arr[i]);
}

}
if (flag == 0)
{
printf("\nSearch Unsuccessful");
}
return 0;
}
Strings
// Program to declare and initialise character
type Array

#include <stdio.h>
int main()
{
char arr[100];
printf("\nEnter the Name:");
scanf("%s", arr);
printf("%s\n", arr );
return 0;
}

/*
scanf alternative
gets(arr);
*/
Formatting output in printf

Description Code Result


printf("\n |%s|",
Normal |Programming|
"Programming");

printf("\n |%5s|",
Width 5 |Programming|
"Programming");

printf("\n |%20s|",
Width 20 | Programming|
"Programming");

Width 20, left printf("\n |%-20s|",


|Programming |
aligned "Programming");

Width 20, only printf("\n |%20.7s|",


| Program|
7 characters "Programming");
Assignment
//0x07

//Use scanf for input in Every Program


//Do not Use In-Built Functions
1. Write a program to print Fibonacci series using
array.

2. Continuation with Assignment 4 Question 4. Write a


program to check whether the triangle can be formed
or not, if yes check whether the triangle is
equilateral or not. Take 3 coordinates as vertices of
Triangle in an array.

Input : 3 coordinates
Output:
If triangle cannot be drawn: no
If triangle can be drawn but not equilateral: yes, Not Equilateral
If triangle can be drawn and equilateral: yes, Equilateral

3. Write a program to Sort an array. (Bubble Sort)

4. Write a program to Search an element in an array.


(Binary Search)

5. Write a program to count number of characters of a


string.

6. Write a program to copy one string to another.

7. Write a program to compare two strings.

8. Write a program to concatenate two strings.


Points to Remember
1. Filetype: .c

2. Naming Convention for File: Question_Y.c


where Y = Question No in that Assignment
example: Question_1.c

3. Write your details in every program

/*
--------------------------------------------------
|Author : Your_Name |
|Roll No: Your_Roll_No |
|Department: Your_Department |
--------------------------------------------------
*/
Character Functions

Library File (ctype.h)

Function Description
Returns a non-zero if c is alphabetic or
ialnum(c)
numeric
isalpha(c) Returns a non-zero if c is alphabetic
Returns a non-zero if c is a control
scntrl(c)
character

isdigit(c) Returns a non-zero if c is a digit, 0 – 9

Returns a non-zero if c is a non-blank but


isgraph(c)
printing character
Returns a non-zero if c is a lowercase
islower(c)
alphabetic character, i.e., a – z
Returns a non-zero if c is printable, non-
isprint(c)
blanks and white space included

Returns a non-zero if c is a printable


ispunct(c)
character, but not alpha, numeric, or blank

Returns a non-zero for blanks and these


isspace(c)
escape sequences:
Returns a non-zero if c is a capital
isupper(c)
letter, i.e., A – Z
Returns a non-zero if c is a hexadecimal
isxdigit(c)
character: 0 – 9, a – f, or A – F
Returns the lowercase version if c is a
tolower(c)
capital letter;
Returns the capital letter version if c is
toupper(c)
a lowercase
String Functions

Library File (string.h)

Function Description
strcpy(s1,s2) Copies s2 into s1

Concatenates s2 to s1. That is, it appends the


string contained by s2 to the end of the string
strcat(s1,s2) pointed to by s1. The terminating null character of
s1 is overwritten. Copying stops once the
terminating null character of s2 is copied.

Appends the string pointed to by s2 to the end of


the string pointed to by s1 up to n characters
long. The terminating null character of s1 is
strncat(s1,s2,n) overwritten. Copying stops once n characters are
copied or the terminating null character of s2 is
copied. A terminating null character is always
appended to s1.

Returns the length of s1. That is, it returns the


strlen(s1) number of characters in the string without the
terminating null character.

Returns 0 if s1 and s2 are the same


strcmp(s1,s2) Returns less than 0 if s1<s2
Returns greater than 0 if s1>s2

strchr(s1,ch) Returns pointer to first occurrence ch in s1

strstr(s1,s2) Returns pointer to first occurrence s2 in s1

You might also like