Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
C programming example that implements a modified string length function (from ECE 2534)
// Example program for first problem on list -- strlen // What happens when the string that you enter is longer than SIZE // Is that what you expect? #include <stdio.h> #define SIZE 30 #define ERROR -1 // I modified the goal a bit // This function tries to determine the length // of a C string. If it is longer than maxSize, // it returns an ERROR. Otherwise, it returns the // length of the string. int strlen(char *str, int maxSize) { int i; for (i=0; i<maxSize; i++) { if (str[i]==0) { return(i); } } return(ERROR); } int main(void) { // my array for the string char myString[SIZE]; int stringLength; printf("Example program for my strlen implmentation\n"); // First read in an input string if (fgets(myString, SIZE, stdin)) { // input has worked, do something with data stringLength = strlen(myString,SIZE); if (stringLength >= 0) { printf("Read in the string \"%s\", length calculated as %d\n", myString, stringLength); } else { printf("Error calculating string length!\n"); } } else { printf("Error reading in the string!\n"); } return 0; }
run
|
edit
|
history
|
help
0
MEMORy eRROR
Matrices de cadenas con *punteros
Factorial_LOOP
C code to compare two texts in form of array of chars ignoring the capitalization
MatrixMul_shortxshort
lab8part2final
test
funciones serie de taylor
Pointer arithmatic
Goodone pointer minus void pointer