如何在C ++中的main函数中输出两个新字符串?

我需要编写一个程序,其中将char数组初始化并拆分为2个没有连接字符的新c样式字符串。当我将void函数用作单独的程序时,它可以工作,但是当我在main函数中调用它时,它就没有作用。我的函数调用方式是否错误或代码编写方式是否有误?该程序的工作方式如下:chara数组被初始化为Frenk + Harizaj,并且void函数检查每个字符,直到到达+,将其分配给sName,然后在+之后继续检查,将其分配给sSurname并输出两个字符串


       #include <iostream>
       using namespace std;

       void splitter(char s[])
       {

           int i, x, counter;
           char sName[20]="";
           char sSurname[20]="";

           for(i=0; s[i]!='\0'; i++)
           {
               counter++;
           }

           for (i=0; i!='+'; i++)
           {
               sName[i]=s[i];

               if(s[i+1]=='+')
               {
                   for(x=i+2; x<counter; x++)
                   {
                       sSurname[x-(i+2)]=s[x];
                   }
                   i=counter;
               }
           }

           cout<<"Name: "<<sName<<endl;
           cout<<"Surname: "<<sSurname<<endl;
       }



       int main()
       {
           int a, b;
           int i=0, j=0, k=0; 

           char s[]="Frenk+Harizaj";

           char firstName[10], lastName[10], fullName[20], character[2];

           cout<<"Press 1 to merge two strings: "<<endl;
           cout<<"Press 2 to split two strings: "<<endl;
           cin>>a;

           if(a==1)
           {
               cout<<"Enter your first name: ";
               cin.ignore();
               cin.getline(firstName, 15);

               cout<<"Enter your last name: ";
               cin.getline(lastName, 15);
               cout<<endl;

               cout<<"Connecting character: "<<endl;
               cout<<"Press 1 for no connecting character "<<endl;
               cout<<"Press 2 to enter connecting character "<<endl;

               cin>>b;

               if(b==1)
               {
                   for(i; firstName[i]!='\0'; i++)
                   {
                       fullName[i]=firstName[i];
                   }


                   for(i, j; lastName[j]!='\0'; i++, j++)
                   {
                       fullName[i]=lastName[j];
                   }

                   cout<<fullName;
               }

               else if(b==2)
               {
                   cout<<"Enter the connecting character: ";
                   cin.ignore();
                   cin.getline(character, 2);

                   for(i; firstName[i]!='\0'; i++)
                   {
                       fullName[i]=firstName[i];
                   }

                   for(i, k; character[k]!='\0'; i++, k++)
                   {
                       fullName[i]=character[k];
                   }

                   for(i, j; lastName[j]!='\0'; i++, j++)
                   {
                       fullName[i]=lastName[j];
                   }

                   cout<<fullName;
               }
           }

           if(a==2)
           {   
               splitter(s);
           }


       }
   ```
评论
xnam
xnam

尽管我可以建议您对代码进行大量更改,例如使用C ++标准字符串而不是C样式字符串,进行适当的边界检查等,但可以解决您的代码的最简单更改是:

(1) Initialize counter variable to 0 in splitter function
(2) Inner for loop is written wrongly as for (i=0; i!='+'; i++). It should be for (i=0; i < counter; i++)

点赞
评论
凹凸Man
凹凸Man

一般来说,当您需要做两个单独的事情时,它们属于两个单独的代码块。特别是,从复制第一部分的循环内部复制输入的后半部分会造成混淆。只需编写两个循环:

// first copy first part of input string
for (int i = 0; i < counter && s[i] != '+'; ++i)
     sName[i] = s[i];
sName[i] = '\0'; // append nul terminator

// then copy second part of input string
if (s[i] == '+') {
    ++i;
    int j = 0;
    for ( ; i < counter; ++i, ++j)
        sSurname[j] = s[i];
    sSurname[j] = '\0';
}

但是这些代码大部分都可以用标准库中的函数代替:

const char *plus_loc = std::strchr(s, '+');
if (plus_loc) {
    int plus_offset = plus_loc - s;
    std::strncpy(sName, s, plus_offset);
    sName[plus_offset] = '\0';
    std::strcpy(sSurname, plus_offset + 1);
}
点赞
评论