locked
Three C++ problem RRS feed

  • 問題

  • Can someone help me to slove three C++ problem. Please~~

     

    2008年11月30日 13:57

解答

  • Do you really need to remove the character(s) in your program?  You may choose to use an index to navigate through the characters in the variable "word" (by incrementing the index from 1 to the number of characters in the variable "word") instead of remove the character.

     

    The issue you've mentioned in your post is the requirements from the assignment sheet.  However, do you think of any algorithm in order to solve the problem?

     

    2008年11月30日 16:36

所有回覆

  • Would you mind share your design/approach for the three problems?  What exactly the technical issues you've got?  I personally don't prefer to answer/provide code for an assignment from school.... sorry about that.  However, if you've got questions/difficulties in your implementation, I think there are many friends here could give you some hints/solutions.
    2008年11月30日 15:24
  • What code could i use to remove a character in a word input my the user in C++??

    2008年11月30日 16:10
  • What data structure you decided to use in order to store the input characters?

    2008年11月30日 16:18
  • For the first task:

    i am going to get the value by

    #include <iostream>

    cin >> word;

    then remove the character one by one and see rather it match the digit word and output the result.

    But i don't how to code it.
    2008年11月30日 16:27
  • Do you really need to remove the character(s) in your program?  You may choose to use an index to navigate through the characters in the variable "word" (by incrementing the index from 1 to the number of characters in the variable "word") instead of remove the character.

     

    The issue you've mentioned in your post is the requirements from the assignment sheet.  However, do you think of any algorithm in order to solve the problem?

     

    2008年11月30日 16:36
  • How could i do that??

    please could you teach me, i had to hand it in very soon. Please help me.
    2008年11月30日 16:46
  • For your question 1a, the main technique you need to know is to navigate through a string/array of characters.  If you know how to navigate through a string/array of characters, then it should be easy for you because you can extract each character from the input string and the string you need to compare, e.g. "ONE", "TWO", etc.

    2008年11月30日 17:25
  • will this work??

     

    char input[20];
    cout << "Enter word\n";
    cin >> input;
    if( strstr(input,"ONE")  != NULL)
    {
       // found it
    }

    2008年11月30日 17:29
  • No, strstr can only match substring with exact spelling and cannot fulfill your requirement.  You have to compare character by character by yourself.

     

    2008年11月30日 17:46
  •  

    Then what can i use? Please help me!

    2008年11月30日 17:50
  • This is what i code so far.

    Please tell me what should i put in.

     

    #include "stdafx.h"

    #include <string.h>

    #include <stdio.h>

    int main ()

    {

    char str[15];

    char result;

    char * digit[9] ={

    "ONE",

    "TWO",

    "THREE",

    "FOUR"

    "FIVE"

    "SIX"

    "SEVEN"

    "EIGHT"

    "NIGHT"

    };

    printf ("Please enter a word",str);

    scanf ("%s",&str);

    isdigitword(str);

    return 0;

    }

    int isdigitword(char *word)

    {

    char * pch;

    pch=strchr(str, digit);

    while (pch!=NULL)

    {

    printf ("found at %d\n",pch-str+1);

    pch=strchr(pch+1);

    }

    return 1; /* change this to return 0 at leisure */

    }

    2008年12月1日 11:30
  • Can someone tell me what wrong in the code please.

    #include "stdafx.h"

    #include <string.h>

    #include <stdio.h>

    bool TextContainsADigit (char, char);

    int main ()

    {

    char str[15];

    char result;

    char * digit[9] ={

    "ONE",

    "TWO",

    "THREE",

    "FOUR",

    "FIVE",

    "SIX",

    "SEVEN",

    "EIGHT",

    "NIGHT"

    };

    printf ("Please enter a word");

    scanf ("%s",&str);

    if(TextContainsADigit(str, digit){

    printf (digit[index - 1] );

    }else{

    printf ("NO");

    }

    return 0;

    }

    bool TextContainsADigit (char* text, char* digit)

    {

    while (*digit != 0)

    {

    if (*text != *digit)

    {

    text++;

    }

    else

    {

    digit++;

    text++;

    }

    if (*text == 0)

    {

    return false;

    }

    }

    return true;

    }

    2008年12月1日 14:15
  • In your second sample code, you are not compare each character from the input and the digit array.  You have to find a way to navigate through input and the digit array character by character.  You can't be lazy to just one or two line of codes to solve the problem.

     

    2008年12月2日 14:54
  • Thanks very much for your help, i finally have sloved the  problrm myself.

    2008年12月3日 19:11