0% found this document useful (0 votes)
7 views

C Strings Structures Notes

Uploaded by

Andy Debnath
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)
7 views

C Strings Structures Notes

Uploaded by

Andy Debnath
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/ 3

Key Notes on Strings and Structures in C

1. Introduction to Strings
A string is a one-dimensional array of characters terminated by a null character '\0'. Strings are used

to manipulate text such as words and sentences in C programming. Example:

char name[] = {'H', 'E', 'L', 'L', 'O', '\0'};

C automatically inserts the null character when initializing strings with double quotes, e.g., char

name[] = "HELLO";

2. Standard Library String Functions


- strlen(): Finds the length of a string.

- strcpy(): Copies one string into another.

- strcat(): Concatenates two strings.

- strcmp(): Compares two strings.

- strrev(): Reverses a string.

- strstr(): Finds the first occurrence of a substring.

Q1: Write a program to find the length of a string using strlen().

Answer:

```

#include <stdio.h>

#include <string.h>

int main() {

char str[] = "Hello World";

printf("Length: %d", strlen(str));

return 0;

```

3. Pointers and Strings


Pointers can be used to manipulate strings more efficiently. You can store the base address of a

string in a pointer and use it to access or modify the string.

Q2: Demonstrate string manipulation using pointers.

Answer:

```

#include <stdio.h>

int main() {

char str[] = "Hello";

char *ptr = str;

while(*ptr != '\0') {

printf("%c ", *ptr);

ptr++;

return 0;

```

4. Structures in C
Structures allow grouping of variables of different types under a single name. Useful in database

management and handling complex data. Structure elements are accessed using the dot (.)

operator.

Q3: Write a structure to store and display information about a book.

Answer:

```

#include <stdio.h>

struct Book {
char title[50];

char author[50];

float price;

};

int main() {

struct Book b1 = {"C Programming", "Author Name", 250.00};

printf("Title: %s\nAuthor: %s\nPrice: %.2f", b1.title, b1.author, b1.price);

return 0;

```

5. Conclusion
Understanding strings and structures in C is crucial for handling complex data and performing

various operations efficiently. Make sure to practice writing and manipulating strings and structures

to master these concepts.

You might also like