0% found this document useful (0 votes)
5 views14 pages

C Arrays and String Handling Basics

The document provides an overview of arrays and strings in C programming, including definitions, initialization methods, and various functions for string handling. It covers topics such as array declaration syntax, multi-dimensional arrays, and functions like strlen() and strcpy(). Additionally, it discusses the differences between character arrays and strings, as well as input methods for strings.

Uploaded by

kasithangamcse92
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)
5 views14 pages

C Arrays and String Handling Basics

The document provides an overview of arrays and strings in C programming, including definitions, initialization methods, and various functions for string handling. It covers topics such as array declaration syntax, multi-dimensional arrays, and functions like strlen() and strcpy(). Additionally, it discusses the differences between character arrays and strings, as well as input methods for strings.

Uploaded by

kasithangamcse92
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

1. What is the starting index of an array ?

An array is a collection of elements stored in contiguous memory locations.


To access these elements, programming languages use an index

******************************************************************
2. What are the different ways of initializing array ?
[Link] initialize an array in C with the same value, the simple way is to provide
an initializer list.

This will initialize the num array with value 1 at all index

[Link] For Loop: We can also use for loop to initialize an array with the same
value.

*************************************************************
3. Name any two libraray function used for string handling.
[Link](): used to find the length of a string.
[Link]():used to copy one string into another.

1.
********************************************************************
***************************
4. What is an array? Write the syntax for multi dimensional array.
An array is a collection of elements of the same data type stored in contiguous
memory locations.
 Each element can be accessed using an index.
 Arrays help in storing multiple values under a single name instead of using
many variables.
we can declare arrays with two or more dimensions.
1. One-Dimensional Array
A 1D array is just a simple list of elements of the same type stored in continuous
memory locations.
Syntax
data_type array_name[size];
Two-Dimensional Array
A 2D array is like a table (matrix) with rows and columns.
data_type array_name[rows][columns];
Multi-Dimensional Array
A multi-dimensional array is an extension of the 2D array, where we can have more
than two dimensions (3D, 4D, etc.).
data_type array_name[size1][size2][size3]...[sizeN];

5. Design a C program for compare any two string.

6. Write down the syntax for array declaration.


An array is a collection of elements of the same data type stored in contiguous
memory. It allows storing multiple values under a single variable name.
Syntax: data_type array_name[size];
int numbers[5];
7. What is the purpose and prototype of the function 'strcpy'?
The strcpy function is used to copy the contents of one string into another. It takes
the source string and copies it into the destination string, including the null character
\0 at the end.
strcpy(destination, source);
Destination → The string (character array) where you want to copy the content.
Source → The string (character array) whose content you want to copy.

8. Declare a float array of size 5 and assign 5 value to it.

********************************************************************
9. Sort the following elements using selection sort method 23,55,16,78,2.
**************************************************************
10. How to identify the length of a string?
The easiest way is to use the strlen() function from the <string.h> library
#include <string.h>

int length = strlen(string_name);

string_name → The string whose length you want to find.


strlen() returns the number of characters before the null character \0.
*******************************************************************1
11. Write the functions used for: Finding string length, Copying one string to another,
Comparing two strings
string length:
strlen(string_name);
Returns the number of characters in the string (excluding the null character \0).
Copying one string to another:
strcpy(destination, source);
string comparison is done using the strcmp() function from the <string.h> library.
#include <string.h>
int result = strcmp(string1, string2);
0 → Strings are equal, <0 → string1 is less than string2, >0 → string1 is greater than
string2
12. Illustrate the difference between gets() and scanf() for string input
scanf("%s", str) reads a string only up to the first space, so it cannot take input
with spaces.
gets(str) reads the entire line including spaces until the Enter key is pressed

13. Differentiate between character array and string with example.


********************************************************************
******************************************************************
14. Write a program to read n numbers into an array and display them in reverse
order.
********************************************************************
*******************************************************************
15. Write a program to calculate the sum and average of elements in an array.
********************************************************************
********************************************************************
16. Develop a C program to find the largest element in an array.
17. Given the array declaration int a[5] = {2,4,6};, explain the status of uninitialized
elements.

18. Assess the efficiency of using strlen() vs manually iterating through characters.
Output:

Manually by
int length = 0;
while (str[length] != '\0')
length++;
**************************************************************
19) define array
An array is a collection of elements of the same data type stored in contiguous
memory locations.
Eg: int arr[6] = {10, 20, 30, 40, 50,60};
 int → data type of elements
 arr → name of the array
 [6] → number of elements (size)

***************************************************************
********************************************************************
20)What is a string?
A string is a sequence of characters terminated by a null character '\0' and is used to
store text in C
Strings are used to store and manipulate text such as words or sentences.
They are declared using the char data type in C.
Example: char name[10] = "hellooo";
********************************************************************
********************************************************************
Part B
Write a C program to check whether the given matrix is symmetric or not.
********************************************************************
What is string? Explain about declaration and initialization of string in ‘C’. How
strings are displayed with different formats? Explain with examples.

A string in C is a sequence of characters that ends with a null character ('\0').


Strings are used to store and manipulate text such as words, names, or sentences.
In C, strings are represented as arrays of characters.

You might also like