Strings – Let us C https://c.praveshagrawal.com Example C Programs Sat, 02 Dec 2023 11:05:29 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Substring Extraction https://c.praveshagrawal.com/2023/12/02/substring-extraction/ https://c.praveshagrawal.com/2023/12/02/substring-extraction/#respond Sat, 02 Dec 2023 11:05:29 +0000 https://c.praveshagrawal.com/?p=64 #include <stdio.h> #include <string.h> int main() { // Substring extraction char text[] = "Programming is fun"; char substring[10]; strncpy(substring, text + 13, 3); // Extract "fun" substring[3] = '\0'; // Null-terminate the substring printf("Original Text: %s\n", text); printf("Extracted Substring: %s\n", substring); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/substring-extraction/feed/ 0 String Copy https://c.praveshagrawal.com/2023/12/02/string-copy/ https://c.praveshagrawal.com/2023/12/02/string-copy/#respond Sat, 02 Dec 2023 11:05:05 +0000 https://c.praveshagrawal.com/?p=62 #include <stdio.h> #include <string.h> int main() { // String copy char source[] = "Hello, World!"; char destination[20]; strcpy(destination, source); printf("Source: %s\n", source); printf("Destination: %s\n", destination); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/string-copy/feed/ 0 String Comparison https://c.praveshagrawal.com/2023/12/02/string-comparison/ https://c.praveshagrawal.com/2023/12/02/string-comparison/#respond Sat, 02 Dec 2023 11:04:45 +0000 https://c.praveshagrawal.com/?p=60 #include <stdio.h> #include <string.h> int main() { // String comparison char str1[] = "apple"; char str2[] = "orange"; if (strcmp(str1, str2) == 0) { printf("The strings are equal.\n"); } else { printf("The strings are not equal.\n"); } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/string-comparison/feed/ 0 String Concatenation https://c.praveshagrawal.com/2023/12/02/string-concatenation/ https://c.praveshagrawal.com/2023/12/02/string-concatenation/#respond Sat, 02 Dec 2023 11:04:25 +0000 https://c.praveshagrawal.com/?p=58 #include <stdio.h> #include <string.h> int main() { // String concatenation char firstName[20] = "John"; char lastName[20] = "Doe"; char fullName[40]; strcpy(fullName, firstName); // Copy the first name to the full name strcat(fullName, " "); // Concatenate a space strcat(fullName, lastName); // Concatenate the last name printf("Full Name: %s\n", fullName); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/string-concatenation/feed/ 0 String Length Calculation https://c.praveshagrawal.com/2023/12/02/string-length-calculation/ https://c.praveshagrawal.com/2023/12/02/string-length-calculation/#respond Sat, 02 Dec 2023 11:04:02 +0000 https://c.praveshagrawal.com/?p=56 #include <stdio.h> #include <string.h> int main() { // String length calculation char text[100]; printf("Enter a string: "); scanf("%s", text); int length = strlen(text); printf("Length of the string: %d\n", length); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/string-length-calculation/feed/ 0 String Input and Output https://c.praveshagrawal.com/2023/12/02/string-input-and-output/ https://c.praveshagrawal.com/2023/12/02/string-input-and-output/#respond Sat, 02 Dec 2023 11:03:39 +0000 https://c.praveshagrawal.com/?p=54 #include <stdio.h> int main() { // String input and output char name[50]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/string-input-and-output/feed/ 0