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

Strings

This C program demonstrates the use of string handling functions such as strlen(), strcmp(), strcat(), strcpy(), strchr(), and strstr(). It takes user input, compares string lengths and equality, concatenates strings, copies a string, searches for characters within strings, and demonstrates that gets() is equivalent to storing user input at the address of the first character of a string array element.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Strings

This C program demonstrates the use of string handling functions such as strlen(), strcmp(), strcat(), strcpy(), strchr(), and strstr(). It takes user input, compares string lengths and equality, concatenates strings, copies a string, searches for characters within strings, and demonstrates that gets() is equivalent to storing user input at the address of the first character of a string array element.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.h> #include <string.

h> int main(void) {

char s1[80], s2[80]; gets(s1); gets (s2); printf("lengths: %d %d\n", strlen(s1), strlen(s2)); if(!strcmp(s1, s2)) printf("The strings are equal\n"); strcat(s1, s2); printf (''%s\n", s1); strcpy(s1, "This is a test.\n"); printf(s1); if(strchr("hello", 'e')) printf("e is in hello\n"); if(strstr("hi there", "hi")) printf("found hi"); return 0;
}

lengths: 5 5 The strings are equal hellohello This is a test. e is in hello found hi Remember, strcmp( ) returns false if the strings are equal. Be sure to use the logical ! operator to reverse the condition, as just shown, if you are testing for equality Arrays of Strings

gets(str_array[2]); The preceding statement is functionally equivalent to gets(&str_array[2][0]);

You might also like