Enter a line of characters to count how many words there are, separated by spaces between the words

Source: Internet
Author: User

Problem Description:

Enter a line of characters to count how many words there are, separated by spaces between the words

Problem Solving Ideas:

To determine whether the word appears, you can use the appearance of the space to judge (a continuous number of spaces to make a), if the current character is a space, indicating that Word does not appear, the current word printable space, the previous character is a space to indicate that the new word appears, count++, before the character is a space, with the status flag word to mark

The code is as follows:

#include <stdio.h>   //printf#include<string.h>  //gets#include<stdlib.h>  //system# Define Maxlenth 1000//character array maximum capacity int main () {System ("title count the number of 9");//Set CMD window title system ("Mode con cols=100 line S=100 ");//Set window width and height system (" color 0A ");                   Set Curtain and font color char string[maxlenth];//a character char *ch = String that holds the terminal input;    Point to the array int count = 0;        Count the number of words int word = 0;         The status flag bit, the new word appears (the current word printable space, before the character is a space) word=1, otherwise word = 0printf ("Please input the charactors you want to count the words:\n"); String), for (; *ch! = ' ++ch ') {if (*ch = = ')    //The current character is ' space ', indicating that the new word does not appear {word = 0;} else if (Word = = 0)//(the current character printable space, preceded by a space) Word=1{count++;word = 1;}} printf ("There is%d words\n", count); return 0;}


It can also be used without pointers, with the following code:

#include <stdio.h>       //printf#include<stdlib.h>      //system#include<string.h>      // Getsint Main () {System ("mode con cols=100 lines=100"); System ("Color 0A"); Char string[100000];//holds a character int num = 0;        Count the number of words int word = 0;       The sign of whether the word appears, 0: The new word does not appear, 1: The new word appears int i;printf ("Please input the charactors so want to Count words:\n"); gets (string); 
   //from the terminal enter a character for which you want to count the number of words for (i=0;string[i]! = ' 0{word '; ++i) {if (string[i] = =)//The current character is a space, indicating that the new word does not appear word = = 0;} else if (Word = = 0)  //The current character is not a space and the previous character is a space, indicating that the new word appears {word = 1;num++;}} printf ("There is%d words in the charactors you put \ n", num); system ("pause"); return 0;}



Enter a line of characters to count how many words there are, separated by spaces between the words

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.