Home > OS >  How can I make it so that my code requires at least 2 strings to be put through the function?
How can I make it so that my code requires at least 2 strings to be put through the function?

Time:11-09

I was tasked with writing code that would convert strings into their initials via a function. For example, running "Dog" and "Cat" through the function would yield an output of "D. C.". However, another component of the task is that the code must require at least 2 strings to run. This is where I'm getting stuck.

Here's my code which works fine, just missing the "require 2 strings to run" part. Any suggestions for optimizing the code would be helpful as well.


#include <iostream>
#include <string>
#include <string.h>
using namespace std;

string initials(string&, string&, string&, string&, string&);

int main()
{
    string a = "This";
    string b = "Is";
    string c = "A";
    string d = "Test";
    string e = "Run";
    initials(a, b, c, d, e);
    cout << a << ". " << b << ". " << c << ". " << d <<  ". " <<e;
}

string initials(string& a, string& b, string& c, string& d, string& e) {
    a = a[0];
    b = b[0];
    c = c[0];
    d = d[0];
    e = e[0];

    return a;
    return b;
    return c;
    return d;
    return e;
}

My code only allows for 5 strings to go through the function, but it also has to require at least 2 for the code to run. The output for the provided code yields "T. I. A. T. R.", which is what I wanted. However, the code below also runs fine. How can I force the code to only run if there are at least two strings run through the function?


#include <iostream>
#include <string>
#include <string.h>
using namespace std;

string initials(string&, string&, string&, string&, string&);

int main()
{
    string a = "This";
    string b;
    string c;
    string d;
    string e;
    initials(a, b, c, d, e);
    cout << a << ". " << b << ". " << c << ". " << d <<  ". " <<e;
}

string initials(string& a, string& b, string& c, string& d, string& e) {
    a = a[0];
    b = b[0];
    c = c[0];
    d = d[0];
    e = e[0];

    return a;
    return b;
    return c;
    return d;
    return e;
}

CodePudding user response:

Welcome to C !

Modern C takes optional parameters. In addition, you should probably revisit the basics of how functions return in the language. Multiple return statements are ignored after the first one.

This should work:

#include <iostream>                                                                                                                                          
#include <string>                                                                                                                                            
#include <string.h>                                                                                                                                          
                                                                                                                                                             
using namespace std;                                                                                                                                         
                                                                                                                                                             
string initials(string first, string second, string third = "", string fourth = "", string fifth = "");                                                      
                                                                                                                                                             
int main()                                                                                                                                                   
{                                                                                                                                                            
    string a = "This";                                                                                                                                       
    string b = "Are";                                                                                                                                        
    string c = "My";                                                                                                                                         
    string d = "Name";                                                                                                                                       
    string e = "Initials";                                                                                                                                   
    cout << initials(a, b, c, d, e) << endl;                                                                                                                 
    cout << initials("John", "Smith") << endl;                                                                                                               
}                                                                                                                                                            
                                                                                                                                                             
string initials(string first, string second, string third, string fourth, string fifth) {                                                                    
  string result = string(1, first.at(0))   ". ";                                                                                                             
  result  = string(1, second.at(0))   ". ";                                                                                                                  
  result  = third == "" ? "" : string(1, third.at(0))   ". ";                                                                                                
  result  = fourth == "" ? "" : string(1, fourth.at(0))   ". ";                                                                                              
  result  = fifth == "" ? "" : string(1, fifth.at(0))   ". ";                                                                                                
  return result;                                                                                                                                             
}

Note that I removed the references & from the function signature. I'll leave it up to you to try what happens if you keep them :)

If you've learned arrays already, you should notice that the implementation of initials could easily be generalized to an array of N names.

CodePudding user response:

variadic template might help for your 2 or more restriction:

void make_initial(std::string& s)
{
    if (!s.empty()) { s.resize(1); }
}

template <typename... Strings>
requires(sizeof...(Strings) >= 2 && (std::is_same_v<std::string, Strings> && ...))
void initials(Strings&... strings)
{
    (make_initial(strings), ...);
}

Demo

  •  Tags:  
  • c
  • Related