Thread: how to modifying a user verified string?

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    how to modifying a user verified string?

    Well, I'm still a beginner in C++, I use microsoft visual C++ as a compiler.
    I want to know how to modify a text that the user has typed 'n it was stored into a string variable, like the following example:

    Code:
    #include 
    int main() 
    { 
    char string[14]; 
    cin.getline(string, 14, '\n'); 
    cout<<string<<endl; 
    return 0; 
    }
    For example, if the user types "you" it appears in the cout as "me"
    It ain't illegal until you get caught..

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    if(string == "you") {
      cout << "me" << endl;
    }
    or if you want to modify it
    Code:
    if(string == "you") {
      string = "me";
    }
    cout << string << endl;

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    alpha, your examples won't work. Even though the poster said they were using a string variable, they were actually using a char array, or c-style string, and you can't set a char array with length 14 to the word "me" which is of length 3(2 plus one for the \0 that is tacked on) or you will get the following error:

    cannot convert from 'char [3]' to 'char [14]'

    Instead, you have to modify a c-style string character by character like this:
    Code:
    string[0] = 'm';
    string[1] = 'e';
    string[2] = '\0';
    for(int i = 3; i<14 ; i++)
    {
        string[i]='\0';
    }
    Similarly, char string[14] that contains "you" will never equal "you" in this conditional:

    if(string == "you")

    You have to use the cstring function strcmp() as Wledge (has now) posted.
    Last edited by 7stud; 08-01-2003 at 04:33 PM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #include <cstring>
    ...
    if(0 == std::strcmp(string,"you"))
    {
        cout << "me" << endl;
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>Instead, you have to modify a c-style string character by character like this:
    strcpy
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    I meant that if the user typed a sentence CONTAINING "you" the same sentence is outputed to the user with the exception of chaning "you" to "me".
    eg. when the user types "as well as you" it is couted as "as well as me"..

    Thanks.
    Memo
    It ain't illegal until you get caught..

  7. #7
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    All right, can't think of any code right now, but I'll give you some pseudo-code and see if you can work it from there.
    Code:
    char input[20], output [20], check[4], ch;
    
    copy what the user types into input;
    
    for( int i = 0; input[i] != '\0')
    
      read one char at at a time into ch;
      if (ch == 'y')
      {
         copy that and the next two chars into check;
         if check == "you"
            append "me" to the end of output;
      }
      else
         append ch to the end of output;
    Well, hope that helps you.
    I know I said no code, but sometimes it's easier to do it with code than write it in human language.
    It might run faster if you put what's in the if in the else and viceversa.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by 7stud
    alpha, your examples won't work. Even though the poster said they were using a string variable, they were actually using a char array, or c-style string, and you can't set a char array with length 14 to the word "me" which is of length 3(2 plus one for the \0 that is tacked on) or you will get the following error:

    cannot convert from 'char [3]' to 'char [14]'

    Instead, you have to modify a c-style string character by character like this:
    Code:
    string[0] = 'm';
    string[1] = 'e';
    string[2] = '\0';
    for(int i = 3; i<14 ; i++)
    {
        string[i]='\0';
    }
    Similarly, char string[14] that contains "you" will never equal "you" in this conditional:

    if(string == "you")

    You have to use the cstring function strcmp() as Wledge (has now) posted.
    Thanks for correcting me. I just looked at the code again. I need to look at code closer.

  9. #9
    Or, instead of getting overly complicated, you could use strstr:

    char *strstr( const char *string, const char *strCharSet );

    Example from help files:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    char str[] =    "lazy";
    char string[] = "The quick brown dog jumps over the lazy fox";
    char fmt1[] =   "         1         2         3         4         5";
    char fmt2[] =   "12345678901234567890123456789012345678901234567890";
    
    void main( void )
    {
       char *pdest;
       int  result;
       printf( "String to be searched:\n\t%s\n", string );
       printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
       pdest = strstr( string, str );
       result = pdest - string + 1;
       if( pdest != NULL )
          printf( "%s found at position %d\n\n", str, result );
       else
          printf( "%s not found\n", str );
    }
    Then just copy the string from before the located word, strcat "me" instead of "you", then strcat the remainder of the string.

    Loop up: strstr, strncpy, strncat.

    strncpy and strncat will copy/append n bytes of your string, which is what you'll need to be doing.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM