Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
advanced string manipulation
#include <stdio.h> #define SIZE 30 /* It is very important to make sure you do not press "Enter" after you feed your string in the input (yellow pane). If you do so, nothing bad happens. However, there will be an "end of line" character at the end of the string. This character is counted toward the length of string. This is not a bug, it's a feature! :-) */ int strlen(char *str) { int i = 0; //search for end of string to find the length of string while (str[i] != 0) { i++; } return (i); } int main(void) { // my array for the string char myString[SIZE]; int i = 0; printf("Example program for some string manipulations.\n\n"); // First read in an input string if (fgets(myString, SIZE, stdin)) { //search for end of string to find the length of string while (myString[i] != 0) { printf("character %c @ index %d\n", myString[i], i); i++; } printf("\nLength of %s is %d using 'while' loop.\n", myString, i); //another method to search for end of string for (i=0; ;i++) { if (myString[i] == 0) break; } printf("Length of %s is %d using 'for' loop.\n", myString, i); printf("Length of %s is %d using a function call.\n", myString, strlen(myString)); } else { printf("Error reading in the string!\n"); } return 0; }
run
|
edit
|
history
|
help
0
b=1,c=1
EXercice
Vereinfachung
B_141202_CTYPE
merge two dice clay to head sport green globe attacked me seven of prime minister fired
A_141211_mayor
18BCE2182 MIDTERM QUES-2
Score
A_141212_IntercambiarValores
test