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
Spring 2017 Project 2 v1.2
hippo he Rhoda shalbert now ring one white car bomb ching
problemapi3
Lab 7 blackjack v0.6
squareifnal
Project 3 Part 1 v1.2
BigInteger maxMinSum
18BCE2182 ASSESS_2 Q2
Stack frame enum test
WrongWay reversible iterator Copyright(C) 2016 Henry Kroll III www.thenerdshow.com