Thread: How to read 2 adjacents strings object

  1. #1
    Registered User
    Join Date
    Aug 2014
    Location
    Italy
    Posts
    5

    How to read 2 adjacents strings object

    I have to solve this programming exercise and I don't know how to do it.
    Write a program to read strings from the standard input, concatenating what is read into a large concatenated string next ( ok I wrote this program and it works properly ), change the program to separate adjacent input strings by a space ( problem with the second part ). In my book i learnt the difference between the string input operator and the getline function, size member function, empty , but i can't undersand how to read two adjacent strings and how to separate them, could you help me ? P.S : I'm a c++ beginner so i know just a very very little bit of the language.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you have already read the strings and formed one large concatenated string, you don't have to "read two adjacent strings". Rather, you just need to separate the adjacent strings that are separated by spaces. One way to do this is to use the find_first_of member function with the substr member function in a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    So the stream operator (>>) reads in 1 word at a time, so that will get you two strings. You can concatenate these back together using +, possibly sticking a space between them.

    Alternatively, do what laserlight suggested with getline for your input.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can also use a stringstream and read words using the >> operator. So many options...!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rules to compare two strings object
    By piero borrelli in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2014, 09:53 AM
  2. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  3. Read strings and bool from txt
    By fukki in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2010, 09:59 AM
  4. how to read strings?
    By happyclown in forum C Programming
    Replies: 10
    Last Post: 01-23-2009, 05:06 AM
  5. Help with Strings -- Please read
    By Drakkath in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2002, 10:01 PM

Tags for this Thread