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

unit-2 c&ds

Uploaded by

pamidi.prameela
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

unit-2 c&ds

Uploaded by

pamidi.prameela
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

A String in C programming is a sequence of characters terminated

with a null character ‘\0’. The C String is stored as an array of


characters. The difference between a character array and a C string is
that the string in C is terminated with a unique character ‘\0’.

C String Declaration Syntax


char string_name[size];

C String Initialization

We can initialize a C string in 4 different ways which are as follows:

1. Assigning a String Literal without Size


String literals can be assigned without size. Here, the name of the string str acts
as a pointer because it is an array.
char str[] = "GeeksforGeeks";
2. Assigning a String Literal with a Predefined Size
String literals can be assigned with a predefined size. But we should always
account for one extra space which will be assigned to the null character. If we
want to store a string of size n then we should always declare a string with a size
equal to or greater than n+1.
char str[50] = "GeeksforGeeks";
3. Assigning Character by Character with Size
We can also assign a string character by character. But we should remember to
set the end character as ‘\0’ which is a null character.
char str[14] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
4. Assigning Character by Character without Size
We can assign character by character without size with the NULL character at the
end. The size of the string is determined by the compiler automatically.
char str[] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};

In C programming, gets() and puts() are functions used for handling strings.
gets()

 Purpose: Reads a line of text from standard input (usually the keyboard) and stores
it in a string.
 Usage: gets(char *str);
 Example:
 char str[100];
 gets(str);
 printf("You entered: %s\n", str);
 .

puts()

 Purpose: Writes a string to standard output (usually the console).


 Usage: puts(const char *str);
 Example:
 char str[] = "Hello, World!";
 puts(str);

C String Functions handaling:


C also has many useful string functions, which can be used to perform certain operations on strings.

To use them, you must include the <string.h> header file in your program:

#include <string.h>
1)String Length:To get the length of a string, you can use
the strlen() function:

Example: #include <stdio.h>

#include <string.h>

int main() {

char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

printf("%d", strlen(alphabet));

return 0;
}

Output:

26

sizeof will always return the memory size (in bytes), and
not the actual string length:
example:

#include <stdio.h>

#include <string.h>

int main() {

char alphabet[50] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

printf("Size is: %d\n", sizeof(alphabet));

return 0;

Output:
Size is: 50

2)Concatenate Strings:
To concatenate (combine) two strings, you can use
the strcat() function:
Example:

#include <stdio.h>

#include <string.h>
int main() {

char str1[20] = "Hello ";

char str2[] = "World!";

// Concatenate str2 to str1 (the result is stored in str1)

strcat(str1, str2);

// Print str1

printf("%s", str1);

return 0;

Output: Hello World!

3)Copy Strings
To copy the value of one string to another, you can use
the strcpy() function:

#include <stdio.h>

#include <string.h>

int main() {

char str1[20] = "Hello World!";


char str2[20];

// Copy str1 to str2

strcpy(str2, str1);

// Print str2

printf("%s", str2);

return 0;

Output: Hello World!

4)Compare Strings
To compare two strings, you can use the strcmp() function.

It returns 0 if the two strings are equal, otherwise a value that is not 0:

#include <stdio.h>

#include <string.h>

int main() {

char str1[] = "Hello";

char str2[] = "Hello";

char str3[] = "Hi";


// Compare str1 and str2, and print the result

printf("%d\n", strcmp(str1, str2));

// Compare str1 and str3, and print the result

printf("%d\n", strcmp(str1, str3));

return 0;

Output:

-4

You might also like