c++


Is there any function in C++ std::string that count total number of same starting characters of two strings or any best way to do


Eg.
abcfgf
abdef
ans=2 {"ab" is a common starting character}
You could use std::mismatch, which returns a pair of iterators indicating the respective iterators where sequences start to differ.
To count the length of common prefixes, for example, you could do the following:
#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>
using namespace std;
int main() {
const string l = "abcde", r = "abcdfgh";
cout << distance(begin(l), mismatch(begin(l), end(l), begin(r)).first) << endl;
}
It seems that there is a buildin function to do this, as shown in this answer.
However, you could do it by simply using a loop (if you want, but would not suggest it), like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abcfgf";
string str2 = "abdet";
int counter = 0;
for(int i = 0; i < str1.size() && i < str2.size(); ++i)
if(str1[i] == str2[i])
counter++;
cout << counter << endl;
return 0;
}
Output:
2

Related Links

OpenGL Renderdoc SSBO / Image
How to transform a range, pair-by-pair with range-v3?
C++ nested class methods
using strcat to concatenate an array of chars [closed]
2D Vector pointer as member of class
Template arguments in constructor of non-template class
Stream operator and abstract class
C++ no cout info displayed
Non duplicate array value in c++
convert int* to int[] in c++
Jsoncpp FastWriter right trim
C++ Ternary operator with scope resolution and condition
Even after many try i am not able to get the complete idea of my c++ code base in office [closed]
Why is the array changed after this function call? [duplicate]
Unexpectedly uninitialised data in call to member function through lambda
Is implementation of double checked singleton thread-safe?

Categories

HOME
videochat
neural-network
processing
google-analytics
ruby-on-rails-4
reporting-services
masonry
unix
ignite
https
umbraco7
servicestack
contiki
visual-foxpro
gemfire
uinavigationcontroller
translate
outlook-redemption
lokijs
affinity
benchmarking
watchkit
ietf-netmod-yang
spring-data-elasticsearch
4d-database
couchbase-view
sqlanywhere
visualvm
unimrcp
jsdata
size-classes
z80
wijmo5
inversifyjs
lwip
inorder
rcp
cassandra-3.0
lektor
bootstrapping
angularjs-ng-model
binary-image
libphonenumber
zlib
winlims
silent-installer
tiles
inputstreamreader
strconv
prime-factoring
csla
rtai
qt4.8
rpath
jodconverter
user-mode-linux
8085
pagerslidingtabstrip
yard
sqlsrv
tess-two
avm2
typescript1.7
acronym
moovweb
savefiledialog
ttml
google-text-to-speech
magnetometer
mysql-error-1093
mooc
duplicate-removal
invalidation
stubs
filtered-lookup
ereg
set-theory
trusted-timestamp
visual-studio-test-runner
memory-mapped-files
32feet
crashrpt
alasset
xml-comments
isaserver

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile