Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
fgets and basic 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
cstructHotel
Name pipe
string.c
BSEARCH() COMPLETE
Spring 2017 Project 2 v1.2
Revprint
day 3 integer manipulation
3,6,8
time clock and day count through second by user input (ratneshgujarathi)
Assignment 5 part 2 v2