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
C_141203_CTYPE
Matrices de cadenas con *punteros
Program 1 - 2D array and memory pointers
String copy
faster posting list using skip pointers
linear hybrid cellular automaton utilizing rules 90 and 150 with period of (2^64)-1 for generating high quality random bits is reversible
Lab 11 v.05
finalpyramid
11
C assignment due sunday