2

I wanted to know if there is any efficient method of getting a string as input and then performing some operation on its characters individually?

Also, after performing operations (check that length of string may also increase or decrease), can we output the new string (string got after performing operations) instead of outputting the characters individually using a for loop?

Note that time is a crucial factor, please provide fastest methods.

2
  • What is your attempt so far? Commented Jun 8, 2014 at 13:46
  • 1
    I took a string as input using getline, found its length and then looped through its characters but then when I have to output the new string, it is to be done character-wise plus the method is not fast enough. Commented Jun 8, 2014 at 13:51

3 Answers 3

1

Is there is any efficient method of getting a string as input and then performing some operation on its characters individually?

Yes, there is: read std::string as usual (say, with std::getline or the >> operator of an input stream), and then access the individual characters in a loop.

std::string str;
std::getline(std::cin, str);
for (int i = 0 ; i != str.size() ; i++) {
    std::cout << "Code of character " << i << " is " << (int)str[i] << std::endl;
}

First demo on ideone.

Also, after performing operations, can we output the new string (string got after performing operations) instead of outputting the characters individually using a for loop?

Yes, you can: std::string is mutable, meaning that you can change it in place.

std::string str;
std::getline(std::cin, str);
for (int i = 0 ; i != str.size() ; i++) {
    if (!std::isalpha(str[i])) {
        str[i] = '#';
    }
}
std::cout << str << std::endl;

Second demo on ideone.

Sign up to request clarification or add additional context in comments.

6 Comments

I did this much thing but is there a way to do this more efficiently? And can I output the new string using write() function?
Why aren't you using std::string::iterator?
Its c++, use "std::cout << yourString << std::endl;" to print
@Mayerz I would definitely use it in production code, but I am not sure if the OP has advanced to studying iterators in his learning of C++, so I decided to stay on the safe side.
@timidgeek This pretty much is as efficient as it gets - there's little opportunity to getting inefficient with accessing string elements.
|
1

You can do it like this:

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string in;
  cout << "Input please\n";
  cin >> in;
  if(in.size() >= 5)
    in[5] = 'A';
  cout << in << "\n";
  return 0;
}

Or you can use std::getline(), instead of std::cin.

Output:

Input please
samaras
samarAs

However, are you sure this is the bottleneck of your program? You can check this with some profiling tools, like the one I use.

[EDIT]

Since OP is asking about efficiency, I did some testing. However, you can to take into account the time that user takes to type the input, but since I am the same person, we can assume this is constant.

So, I did modified a bit a code from another answer, like this:

  std::string str;
  cout << "Input please\n";
  std::getline(std::cin, str);
  if (str.size() >= 5) {
    str[5] = '#';
  }

  std::cout << str << "\n";

Output:

Input please
Samaras
Samar#s
It took me 1.04237 seconds.

And with my code, I got

Input please
Samaras
SamarAs
It took me 0.911217 seconds.

Which actually show that they are pretty close and I would say the difference is due to my typing speed.

I did the timings with std::chrono, like the code I have in my pseudo-site.

2 Comments

Someone asking this question doesn't need profiling tools imho.
When you want to make your code more efficient, you should try to optimize the bottleneck of your project.
0

Basic operation... Some search on the internet could have helped you but here you go...

std::string processStr(const std::string &str) 
{
  for (std::string::iterator it = str.begin(); it != str.end(); ++it)
    // process your string (getting a char is done by dereferencing the iterator
    //  like this: *it
  return (str);
}

2 Comments

How do I output the string? Also, consider that the length of string may vary.
This solution adapt to any length. you can output the returned str like this: std::cout << processStr("blabla") << std::endl;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.