C Tutorial
This program is used to find the string length using strlen function which is available in string library.
Need to include string header to use string len function in c program.
#include<stdio.h> #include<string.h> void main() { char str[] = "testing codingpointer.com"; int len; len = strlen(str); printf("%s length: %d\n", str, len); }
Output:
$ cc string-length.c $ ./a.out testing codingpointer.com length: 25
Defined logic to find the string length using for lopp.
#include<stdio.h> #include<string.h> void main() { char str[] = "testing codingpointer.com"; int len; for(len=0;str[len];len++); printf("total strings: %d\n", len); }
Output:
$ cc string-length.c $ ./a.out testing codingpointer.com length: 25
string is also defined using pointer instead of array, then we can see here how to find string length using strlen function.
#include<stdio.h> #include<string.h> void main() { char *str = "testing codingpointer.com"; int len; len = strlen(str); printf("total strings: %d\n", len); }
Output:
$ cc string-length.c $ ./a.out testing codingpointer.com length: 25
pointer is used to define the string and while loop is used to compute the string length instead of using predefined strlen function.
#include<stdio.h> #include<string.h> void main() { char *str = "testing codingpointer.com"; int len = 0; while(*str++) { len ++; } printf("total strings: %d\n", len); }
Output:
$ cc string-length.c $ ./a.out testing codingpointer.com length: 25
#include<stdio.h> #include<string.h> void main() { char *str = "testing codingpointer.com"; int len; for(len=0;*str++;len++); printf("total strings: %d\n", len); }
Output:
$ cc string-length.c $ ./a.out testing codingpointer.com length: 25
#include<stdio.h> #include<string.h> void main() { char str[] = "testing codingpointer.com"; int len = 0; while(str[len]) { len ++; } printf("total strings: %d\n", len); }
Output:
$ cc string-length.c $ ./a.out testing codingpointer.com length: 25
This c program is used to find the string length for list of strings using for loop and strlen function.
#include<stdio.h> #include<string.h> void main() { char *str[] = {"testing", "codingpointer.com", "c program"}; int i, len; len = sizeof(str)/sizeof(str[0]); printf("total strings: %d\n", len); for(i=0; i<len; i++) { printf("%s length: %d\n", str[i], strlen(str[i])); } }
$ cc string-length.c $ ./a.out total strings: 3 testing length: 7 codingpointer.com length: 17 c program length: 9
Above program can be converted into single one line statement to copy a string to another string array variable.
#include<stdio.h> #include<string.h> void strcopy(char *source, char *destination) { while(*destination++ = *source++); } void main() { char str1[] = "testing codingpointer.com"; char str2[30]; strcopy(str1, str2); printf("\n%s", str2); }
Output:
$ cc string-copy.c $ ./a.out testing codingpointer.com[
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page