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

Strings

Uploaded by

mukuladlakha94
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Strings

Uploaded by

mukuladlakha94
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Strings

Declaring and Initializing String Variables:


• In C, strings are typically represented as arrays of characters. To declare and initialize a
string variable, you declare an array of characters and assign a string literal to it.
• Example:
• #include <stdio.h>

int main() {
// Declaring and initializing a string variable
char my_string[] = "Hello, World!";
printf("%s\n", my_string); // Output: Hello, World!
return 0;
}
Reading and Writing Strings to Screen:
Input/output operations in C are performed using printf() and scanf() functions from the
standard I/O library.
To read a string from the user, you use scanf() with %s format specifier.
Example:
#include <stdio.h>

int main() {
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s\n", name);
return 0;
}
Arithmetic Operations on Characters
#include <stdio.h>

int main() {
char letter = 'A';
letter = letter + 1; // Now letter contains 'B'
printf("%c\n", letter); // Output: B
return 0;
}
String-handling Functions
In C, string-handling functions are available in the string.h library.

Functions like strlen(), strcpy(), strcat(), etc., are commonly used for string
manipulation.
#include <stdio.h>
#include <string.h>

int main() {
char my_string[] = "Hello, World!";
printf("Length of the string: %d\n", strlen(my_string));
printf("Uppercase string: %s\n", strupr(my_string));
return 0;
}
Example (without using built-in functions):
strcpy() - Copies a string:
#include <stdio.h>
#include <string.h>

int main() {
char source[] = "Hello";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
strcat() - Concatenates two strings:
• #include <stdio.h>
• #include <string.h>

• int main() {
• char str1[20] = "Hello, ";
• char str2[] = "World!";
• strcat(str1, str2);
• printf("Concatenated string: %s\n", str1);
• return 0;
•}
strcmp() - Compares two strings:
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2);
if (result == 0)
printf("Strings are equal\n");
else if (result < 0)
printf("str1 is less than str2\n");
else
printf("str1 is greater than str2\n");
return 0;
}
strchr() - Returns a pointer to the first
occurrence of a character in a string:
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
char *ptr = strchr(str, 'W');
if (ptr != NULL)
printf("Found at index: %ld\n", ptr - str);
else
printf("Not found\n");
return 0;
}
strstr() - Returns a pointer to the first
occurrence of a substring in a string
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
char *ptr = strstr(str, "World");
if (ptr != NULL)
printf("Found at index: %ld\n", ptr - str);
else
printf("Not found\n");
return 0;
}

You might also like