string

This is a discussion on string within the C++ Programming forums, part of the General Programming Boards category; ref: seperated strings into pair The problem is enter a string and seperate into space. 1) if char1==char2 replace char2 ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    string

    ref: seperated strings into pair

    The problem is enter a string and seperate into space.
    1) if char1==char2 replace char2 with 'x' LL ->Lx
    2) if one of char not in pair add 'x' to single char eg he ll o-> ox

    Here is my code for it can anyone please show me how to improve the code, thank you?

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    using namespace std;

    int main()
    {
    string input;//input string
    char ch1, ch2;//ch1 and ch2 (ch1 is the first letter of pair)
    int length;//length of input string
    int count=0;
    char pairList[count][2];//combine pairs
    char encrypt[2];//store the encrypted pairs

    int pairCount=0;//count number of pairs
    int pairIndex=0;

    cout<<"Please enter string: ";
    getline(cin,input);//get lines of string

    length = input.length();

    //sepearte input string into non encrypted pairs
    for (int i=0; i<=length; i=i+2)
    {
    ch1 = input[i];
    ch2 = input[i+1];
    pairCount++;

    //put 'x' into pair depends on one of the condition
    if (ch1==ch2)
    ch2='x';
    if (isspace(ch2))//if ch2 is space
    ch2='x';
    if (isspace(ch1))
    ch1=='x';
    cout<<ch1<<ch2<<endl;
    }//end loop

    system("pause");
    return 0;
    }//end main

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Insert code tags. And what is your program trying to do? I can't understand you.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i think you're trying to replace the identical letters in the two strings with an 'x' ?

    Code:
    for(i=0;i<strlen(ch1);i--)
       ch2[i]=(ch1[i]==ch2[i]?'x':ch2[i]);
    sorry if that doesn't help... i didn't get it either...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Might want to make the condition in the for loop something to the effect of:

    i < min(strlen(ch1), strlen(ch2));

    just to make sure you don't compare against garbage values.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    oh yeah... i forgot the precondition for that was:

    strlen(ch1)<=strlen(ch2)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21