Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fgets and String Stuff
#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
Atomic Openmp
string_ptr_to_fct_param
Lab 10 v1
Project 3 part 2 Book v10.2
Factorial_LOOP
pruebapi2
hwk2
joseph triangle
part2final
DUE TUESDAY