words

Reading a sentence until ENTER key pressed using 2-D char array

与世无争的帅哥 提交于 2021-02-07 03:28:57
问题 I need to read a sentence word by word until "ENTER" key is pressed. I used a do..while loop to read words until ENTER key is pressed. Please suggest me some conditions for checking ENTER key press (or) others ways for reading similar input. #include<iostream> #include<string.h> using namespace std; int main(){ char a[100][20]={'\0'}; int i=0; do{ cin>>a[i++]; } while( \\ Enter key is not pressed ); for(int j= 0 ; j < i ; j++) cout<< a[j] << " "; return 0; } 回答1: while (cin.peek() != '\n') {

C - Counting words, characters and lines in file. Character count

烂漫一生 提交于 2021-01-28 00:57:48
问题 I have to write a code in C, which outputs the number of characters, lines and words in a given file. The task seems to be simple, but I'm really not sure what went wrong at this point. So, here's the code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { FILE *file; char filename[256]; char ch; char prevch; int lines=0; int words=0; int characters=0; printf("Enter your filename (don't forget about extension!):\n"); scanf("%s", filename); file=fopen(filename, "r"); if

Php check if string contains multiple words

安稳与你 提交于 2020-05-27 08:33:13
问题 I have looked around the internet for something that will do this but it will only work with one word. I am trying to build a script that will detect a bad username for my site, the bad username will be detected if the username contains any of the words in an array. Here's the code I made, but failed to work. $bad_words = array("yo","hi"); $sentence = "yo"; if (strpos($bad_words,$sentence)==false) { echo "success"; } If anybody could help me, I would appreciate it. 回答1: use substr_count for