unixia
unixia

Reputation: 4320

Taking string as input but dealing with characters individually in C++

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.

Upvotes: 2

Views: 178

Answers (3)

gsamaras
gsamaras

Reputation: 73366

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.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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.

Upvotes: 1

user1585121
user1585121

Reputation:

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);
}

Upvotes: 0

Related Questions