Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Include only what you need

#Include only what you need ThisThis program uses only C++ Standard Library, so we can omit

Interface

#Interface dupcheck() accepts a string by value, but only reads it. That means we should consider passing a reference to constant string, which will avoid copying the string's contents:

#Avoid fixed-size storage

Avoid fixed-size storage

Be careful with braces

#Be careful with braces GCCGCC warns that the indentation is misleading here:

Don't write to cout in functions

#Don't write to cout in functions InsteadInstead of writing to std::cout, we could accept a std::ostream& parameter, or we could return a suitable data structure containing the results, and allow the caller to decide what and how to print it.

Alternative method

#Alternative method WeWe can use a standard container as a "bag" (also known as a "multiset"), and populate it directly from the string. Then remove from the contents any character that appears only once.

#Include only what you need This program uses only C++ Standard Library, so we can omit

#Interface dupcheck() accepts a string by value, but only reads it. That means we should consider passing a reference to constant string, which will avoid copying the string's contents:

#Avoid fixed-size storage

#Be careful with braces GCC warns that the indentation is misleading here:

#Don't write to cout in functions Instead of writing to std::cout, we could accept a std::ostream& parameter, or we could return a suitable data structure containing the results, and allow the caller to decide what and how to print it.

#Alternative method We can use a standard container as a "bag" (also known as a "multiset"), and populate it directly from the string. Then remove from the contents any character that appears only once.

Include only what you need

This program uses only C++ Standard Library, so we can omit

Interface

dupcheck() accepts a string by value, but only reads it. That means we should consider passing a reference to constant string, which will avoid copying the string's contents:

Avoid fixed-size storage

Be careful with braces

GCC warns that the indentation is misleading here:

Don't write to cout in functions

Instead of writing to std::cout, we could accept a std::ostream& parameter, or we could return a suitable data structure containing the results, and allow the caller to decide what and how to print it.

Alternative method

We can use a standard container as a "bag" (also known as a "multiset"), and populate it directly from the string. Then remove from the contents any character that appears only once.

Blockquote extracts from original function
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322

#Avoid fixed-size storage char temp[1024]={0}; strncpy(temp,usr.c_str(),sizeof(temp));

 char temp[1024]={0};
 strncpy(temp,usr.c_str(),sizeof(temp));
 if(temp[i]==temp[j])
 count++;
 i=j;
 if(temp[i]==temp[j])
 count++;
 i=j;

#Avoid fixed-size storage char temp[1024]={0}; strncpy(temp,usr.c_str(),sizeof(temp));

 if(temp[i]==temp[j])
 count++;
 i=j;

#Avoid fixed-size storage

 char temp[1024]={0};
 strncpy(temp,usr.c_str(),sizeof(temp));
 if(temp[i]==temp[j])
 count++;
 i=j;
Rollback to Revision 1
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322

#Avoid fixed-size storage char temp[1024]={0}; strncpy(temp,usr.c_str(),sizeof(temp));

char temp[1024]={0};
strncpy(temp,usr.c_str(),sizeof(temp));
if(temp[i]==temp[j])
 count++;
 i=j;

#Avoid fixed-size storage

char temp[1024]={0};
strncpy(temp,usr.c_str(),sizeof(temp));
if(temp[i]==temp[j])
 count++;
 i=j;

#Avoid fixed-size storage char temp[1024]={0}; strncpy(temp,usr.c_str(),sizeof(temp));

if(temp[i]==temp[j])
 count++;
 i=j;
Minor formatting
Source Link
Stephen Rauch
  • 4.3k
  • 12
  • 24
  • 36
Loading
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /