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
Lab 9 v0.9
la wea afortunada
1.2 No. of Threads
time.c
Ad
150113_ArrayOrdenado
object-oriented C by Henry Kroll III www.thenerdshow.com
CreverseInt
Largest and smallest
FirstCProg