1. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 1
DHANALAKSHMI COLLEGE OF ENGINEERING
Tambaram, Chennai
Department of Computer Science and Engineering
OCS752 INTRODUCTION TO C PROGRAMMING
Year / Sem : IV / VII
2 Marks Q & A
2. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 2
UNIT – III
STRINGS
Introduction to Strings – Reading and writing a string – String operations (without using built-in string
functions): Length – Compare – Concatenate – Copy – Reverse – Substring – Insertion – Indexing –
Deletion – Replacement – Array of strings – Introduction to Pointers – Pointer operators – Pointer
arithmetic – Exercise programs: To find the frequency of a character in a string – To find the number of
vowels, consonants and white spaces in a given text – Sorting the names
PART – A
1. Define – Strings
Strings or character array is defined as the group of characters, digit and symbols enclosed within a
quote. Strings are always terminated with “0” (NULL) character. The compiler automatically adds “0”
at the end of the strings.
2. What is the use of ‘0’ character?
When declaring character arrays (strings), “0” (NULL) character is automatically added at the end of the
string. The “0‟ character acts as an end of character array.
3. Give an example for initialization of string array. (N/D – 14)
Program for initialization of string array
#include<stdio.h>
void main()
{
char city[9]={'N','E','W','Y','O','R','K'};
printf("%s",city);
}
Output:
NEWYORK
4. List the important string handling functions in C.
The important strings handling functions in C
1) strcat() – concatenates two strings
2) strcmp() – compares two strings
3) strcpy() – copies one string over other
4) strlen() – finds length of a string
5) strrev() – reverse the string
3. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 3
5. What is the use of strcmp()?
strcmp() function compares two strings to find whether they are same or different. If two strings are
equal, then it returns a zero otherwise numeric difference between the non-matching character.
Syntax: strcmp(string1,string2);
6. Write a C function to compare two strings. (A/M – 18, A/M – 19)
Program to compare two strings using function
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
gets(a);
gets(b);
if (strcmp(a,b) == 0)
printf("The strings are equal.n");
else
printf("The strings are not equal.n");
}
Output:
xx xx
The strings are equal
7. What is the use of strlen() and strcat() ?
strlen() function is used to count and return the number of character present in a string.
Syntax: len=strlen(string);
strcat() function is used to concatenate or combine two strings together then forms a new string.
Syntax: strcat(str1,str2);
8. What are strrev() and strcpy() function? Write its syntax.
strrev() function is used to reverse a string. This function takes and returns only one argument.
Syntax: strrev(str);
strncpy() is used to copy some portion of one string to another string.
Syntax: strncpy(str1 , str2 ,4) // It copies first 4 characters of str2 to str1.
9. How strings are represented in C language? (A/M - 10)
Strings in C are represented by arrays of characters. The end of the string is marked with a special
character, the null character, which is simply the character with the value 0. (The null character has no
4. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 4
relation except in name to the null pointer. In the ASCII character set, the null character is named NUL.)
Ex.: char string1[] = "Hello, world!";
10. Distinguish between string and array.
S. No. String Array
1 String can hold only char data type Array can hold any data type
2
String size can be changed if it is a
character pointer
Array size cannot be changed
3
The last character of string is a null (“0”)
character
The last element of an array is an element
of the specific type
11. What is a Pointer? How a variable is declared to the pointer? (A/M – 13)
Pointer is a variable which holds the address of another variable. A variable can be declared to the
pointer by using the following syntax
Syntax:
data_type *variable_name;
Example:
int *x, a=5;
x=&a;
12. List the pointer operators.
S. No. Operator Meaning Description
1 * Value at Operator Gives values stored at address
2 & Address Operator Gives address of variable
13. What is the output of the following program? (A/M – 15)
main ()
{
int a=8, b=4, c, *p1=&a, *p2=&b;
c=*p1**p2-*p1/ *p2+9;
printf (“%d”,c); }
Output
c: 39
14. When is null pointer used? (A/M – 18)
Null pointer is used to initialize a pointer variable when that pointer variable is not assigned to any valid
memory address.
5. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 5
15. What is pointer to a pointer?
Pointer to a pointer is a pointer variable points another pointer value. It is mainly used in dynamic
memory allocation. Multiple level of allocation can be accomplished by using pointer to a pointer
variable.
Example:
int *p1, **p2,v=10;
P1=&v; p2=&p1;
Here p2 is a pointer to a pointer.
16. What are the uses of pointer?
Uses of pointers
1) Used to return more than one value to the function
2) More efficient in handling the data in arrays
3) Reduce the length and complexity of the program
4) Allow management of structures which are allocated memory dynamically
17. What will be output of the following program?
#include<stdio.h>
int main()
{
int n=20;
printf(“n %d “, n);
printf(“n %u“, &n);
printf(“n %d “, *(&n));
}
Output
20
1260034844
20
18. What are the possible arithmetic operations on pointers in C language?
Possible arithmetic operations on pointers in C language
1) Increment
2) Decrement
3) Subtraction
4) Comparison
6. OCS752 − Introduction to C Programming VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 6
19. What is pointer arithmetic?
One of the uses of pointer is pointer arithmetic. Like an ordinary variable, pointer variable can also be
used in arithmetic expressions. Assume x, y and z are pointer variables, and the values are 10, 20, 30
respectively. Then the following is an example of pointer expression.
Expression Result
C =*y + 10 Value of c is 30
C = *z + *y Value of c is 50
C = *x + 10 + *y Value of c is 40
C = ++*x Value of c is 11
20. List the applications of pointers.
Applications of pointers
1) To implement data structures
2) Dynamic memory allocation
3) To pass arguments by reference
4) To do system level programming where memory addresses are useful