just use fgets() from file stdio.h (not gets: it is security hole)
Console.WriteLine("Please input a string:"); string str = Console.ReadLine(); Console.WriteLine("Number of characters: " + str.Length);
// get input char input[256]; gets(input); // print one character on each line int i; for(i = 0; input[i] != '\0'; ++i) { printf("%c\n", input[i]); }
std::string input = ""; std::getline (std::cin, input); // get input from stdin std::stringstream ss (input); // place input in a string stream integer num = 0; if (ss >> num) // extract integer from string stream { // Success! } else { // Fail! }
You mean read from file/standard input? With function fgets.
its simple in c and c++: mainly it depends on what type of answer ur looking track fro the user .. if its a numeric u can define a integer or float variable else a string variable to store characters . ex : printf("\n whats ur age ?"); scanf("%d",&age); printf("\n hey ur %d years old :",age); cout
int main() { std::string first, last; std::cout << "Enter your first name: "; std::cin >> first; std::cout << "Enter your last name: "; std::cin >> last; }
/*We can calculate this with a lot of methods I'll explain only one of them */ *str = "abcd"; // this is a input string printf("%d", strlen(str));
import java.util.*; public class Example { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter a string:"); String input = in.next(); System.out.println("The String you entered is: " + input); } }
The charAt function returns the number of occurrences of a specified character in the input string.
#include<iostream> #include<string> int main() { std::string input, invert; bool ok = false; while (!ok) { std::cout << "Enter a 5-digit binary number:"; std::getline (std::cin, input); if (input.size()==5) { invert = input; ok = true; for (auto c = invert.begin(); c!=invert.end(); ++c) { switch (*c) { case ('1'): *c = '0'; break; case ('0'): *c = '1'; break; default: ok = false; } } } if (!ok) { std::cout << "Bad input\n"; invert.clear(); input.clear(); } } std::cout << "Input:\t" << input << std::endl; std::cout << "Invert:\t" << invert << std::endl; }
Scanner scan = new Scanner("[input method]"); // input method could they key board //(System.in) or a String String one = scan.next() + scan.next(); // get the first 2 words and concatenate String two = scan.next(); // the remaining word
Start Declarations String Guess Num Score String PROMPT1 "Computer thinks of an animal, what is it" String PROMPT2 "No! It has four legs" String PROMPT3 "No! It has Fur" String PROMPT4 "No! It likes fish" String PROMPT5 "No! It lives in the north" String PROMPT6 "No! It is white" String PROMPT7 "No! Answer is (Animal)" String PROMPT8 "Would you like to play again" Animal = Polar Bear String Answer = "yes", "no" While PlayAgain = yes Output PROMPT1 Input Guess If Guess=Animal Win() Else Output PROMPT2 Input Guess If Guess = Animal Win() Else Output PROMPT3 Input Guess If Guess = Animal Win() Else Output PROMPT3 Input Guess If Guess = Animal Win() Else Output PROMPT4 Input Guess If Guess = Animal Win() Else Output PROMPT5 Input Guess If Guess = Animal Win() Else Output PROMPT6 Input Guess If Guess = Animal Win() Else Output PROMPT7 End If End If End If End If End If End If End If Output PROMPT8 Input Answer EndWhile Stop Win() Score = Score+1 Return