Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Lab Report-10

Background Theory:
 Introduction to string:
 In C programming, a string is a sequence/array of characters terminated
with a null character \0 . For example: char c[] = "c string"; When the
compiler encounters a sequence of characters enclosed in the double
quotation marks, it appends a null character \0 at the end by default.

 What is the role of the null character at the end of string?


 This helps programmers determine the length of string. Puts function
works by the help of null character. Puts function prints the array of
character until and unless null character appears.

 String handeling function: These functions are defined under the header
file string.h .These functions are designed for strings.
1) strlen(): This function basically counts the length of the string.
Eg: If char ch[50]= “Kathmandu” and int len=stelen(ch) then the
value of len will be 9.
2) strcat(): This function helps us to concatenate two strings.
Syntax: strcat(destination string,source string)
Eg:
#include <stdio.h>

#include <string.h>
int main() {
   char str1[100] = "Hello ", str2[] = "everyone";
   strcat(str1, str2);

   puts(str1);
   puts(str2);

   return 0;
}

Output:

3) strcpy(): This function helps to copy the string.


Syntax: strcpy(destination string, source string)
Eg:
#include <stdio.h>
#include <string.h>
int main() {
   char str1[100] = "Hello ", str2[] = "everyone";
   strcpy(str1, str2);

   puts(str1);
   puts(str2);

   return 0;
}

Output:

4) strcmp(): This function helps us to compare two strings.

Syntax: strcmp(string1, string 2)

Return Value from strcmp()

Return Value Remarks

0 if strings are equal

1 if the first non-matching character in str1  is greater (in ASCII) than that

of  str2 .

-1 if the first non-matching character in str1  is greater (in ASCII) than that

of  str2 .
 Two dimensional array in string: In c programming, we can actually
create two dimensional array of string. This will help us storing words in
different places. Application of it is we can extract word by word from a
sentence and perform the required task.

 Pointer to string: We can create a pointer variable that can point to a


string. Actually, when we try to point towards a string, it basically points
to the base address of the string instead of pointing towards the whole
string. But we can actually retrieve all the characters of the string using
the concept of pointer arithematic.

Analysis and Discussion

Here, during the lab report, i understood about string, how string is stored in
the variable, how string pre-defined function like strcmp, strlen works. Also I
learned how to sort strings in different orders.

Conclusion

Hence, Through Lab report, I got to clear my confusions regarding string and
debugged my mistakes. And from now, I am more confident on writing and
Understand C -Program.

You might also like