ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Strings in C

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='28' AND `tutorial_submenu`='1649' AND `tutorial_status`=1 LIMIT 1

Strings in C

? Strings in C

In C, strings are arrays of characters terminated by a null character (\0).

c

char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};


? Declaring Strings

c

char name[10] = "Alice";

Or:

c

char name[] = {'A', 'l', 'i', 'c', 'e', '\0'};


? Reading Strings

c

char name[20];scanf("%s", name); // stops at whitespace

To read full lines (with spaces), use:

c

fgets(name, sizeof(name), stdin);


?? Printing Strings

c

printf("Name: %s\n", name);


? Common String Functions (from <string.h>)


FunctionDescription
strlen(str)Returns length of the string
strcpy(dest, src)Copies one string to another
strcat(dest, src)Concatenates strings
strcmp(s1, s2)Compares two strings
strrev(str)Reverses string (not standard C)
strchr(str, ch)Finds first occurrence of char
strstr(s1, s2)Finds substring


? Example

c

#include #include <string.h>int main() { char str1[20] = "Hello"; char str2[] = "World"; strcat(str1, str2); // str1 becomes "HelloWorld" printf("Combined: %s\n", str1); printf("Length: %lu\n", strlen(str1)); return 0;}


?? Notes

  • Always ensure your strings are null-terminated.

  • Be careful with buffer overflows when using functions like strcpy.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql