Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我想打印单词的缩写,但不知道我做错了什么_C++_Arrays - Fatal编程技术网

C++ 我想打印单词的缩写,但不知道我做错了什么

C++ 我想打印单词的缩写,但不知道我做错了什么,c++,arrays,C++,Arrays,我想打印这个词的缩写 编写一个C程序,将用户提供的组织名称作为字符串。您必须在该字符串上实现以下函数: 例如: Word=巴基斯坦国家石油公司 缩写:PSO 只有单词的大写字母 它只印了一个字 代码: #include <iostream> using namespace std; void printAbbrevation(char Word[], char WordAbb[]); int findLength(char Word[]) { int length = 0;

我想打印这个词的缩写 编写一个C程序,将用户提供的组织名称作为字符串。您必须在该字符串上实现以下函数:
例如:
Word=巴基斯坦国家石油公司
缩写:PSO 只有单词的大写字母

它只印了一个字

代码:

#include <iostream>
using namespace std;

void printAbbrevation(char Word[], char WordAbb[]);
int findLength(char Word[])
{
    int length = 0;

    for (int i = 0; Word[i] != '\0'; i++)
    {
        length++;
    }
    return length;
}

void findAbbrevation(char Word[])
{
    int WordLength = findLength(Word);
    char WordAbb[] = {0};
    
    for (int i = 0; i < WordLength; i++)
    {
        if (Word[i] >= 'A' && Word[i] <= 'Z')
        {
            WordAbb[i] = Word[i];
        }
    }
    printAbbrevation(Word, WordAbb);
}

void printAbbrevation(char Word[], char WordAbb[])
{
    int abbword = findLength(WordAbb);
    cout << "Abbrevation of " << Word << " is = ";

    for (int i = 0; i < abbword; i++)
    {
        cout << WordAbb[i];
    }
    
}

int main()
{
    char Word[100] = {0};
    cout << "Enter the Word = ";
    cin.getline(Word,100);
    cout << "Length of Word is = ";
    cout << findLength(Word);
    cout << endl;
    findAbbrevation(Word);
    return 0;
}
#包括
使用名称空间std;
无效打印缩写(字符字[],字符字缩写[]);
int findlelength(字符字[])
{
整数长度=0;
for(int i=0;Word[i]!='\0';i++)
{
长度++;
}
返回长度;
}
void findabbreevation(字符词[])
{
int WordLength=findlelength(字);
char-WordAbb[]={0};
for(int i=0;i如果(Word[i]>='A'&&Word[i]因为你使用的是
C++
,而不是
C
,我建议你使用
string
而不是
char[]
,因为用这种方式处理句子和进行操作可能更容易,特别是如果你来自更高级的语言,或者如果你是个新手

如果要打印字符串中每个单词的第一个字母,可以按如下方式遍历字符串:

void printFirstLetter(string sentence) { 
    int i; 
  
    for (i = 0; i < sentence.length(); i++)  
    { 
        //Print the first letter
        if (i == 0) 
            cout<<sentence[i]; 
  
        // Find the space separating the words, then print the next first letter again.
        if (sentence[i] == ' ')  
        { 
            cout<<sentence[i-1]<<" "<<sentence[i+1]; 
        } 
    } 
} 
void findAbbrevation(char Word[])
{
    int WordLength = findLength(Word);
    char WordAbb[25] = {0};
    int j = 0;
    
    int flag = 0;
    for (int i = 0; i < WordLength; i++)
    {
        // To continue untill we get a space
        // this marks the start of the new word
        if(flag)
        {
            if(Word[i] == ' ')
            {
                flag = 0;
            }
            continue;
        }
        else
        {
            if (Word[i] >= 'A' && Word[i] <= 'Z')
            {
                WordAbb[j++] = Word[i];
                flag = 1;
            }
        }
    }
    printAbbrevation(Word, WordAbb);
}
void printFirstLetter(字符串句子){
int i;
对于(i=0;i<句子长度();i++)
{ 
//打印第一个字母
如果(i==0)

您是否需要更改
findabbreation()
函数,如下所示:

void printFirstLetter(string sentence) { 
    int i; 
  
    for (i = 0; i < sentence.length(); i++)  
    { 
        //Print the first letter
        if (i == 0) 
            cout<<sentence[i]; 
  
        // Find the space separating the words, then print the next first letter again.
        if (sentence[i] == ' ')  
        { 
            cout<<sentence[i-1]<<" "<<sentence[i+1]; 
        } 
    } 
} 
void findAbbrevation(char Word[])
{
    int WordLength = findLength(Word);
    char WordAbb[25] = {0};
    int j = 0;
    
    int flag = 0;
    for (int i = 0; i < WordLength; i++)
    {
        // To continue untill we get a space
        // this marks the start of the new word
        if(flag)
        {
            if(Word[i] == ' ')
            {
                flag = 0;
            }
            continue;
        }
        else
        {
            if (Word[i] >= 'A' && Word[i] <= 'Z')
            {
                WordAbb[j++] = Word[i];
                flag = 1;
            }
        }
    }
    printAbbrevation(Word, WordAbb);
}
void findabbreevation(字符词[])
{
int WordLength=findlelength(字);
char-WordAbb[25]={0};
int j=0;
int标志=0;
for(int i=0;i如果(Word)[i] >=“a”和“word”[i] ,可以使用现代C++。
#include <iostream>
#include <sstream>
#include <string>

const std::string findAbbrivation(const std::string& sentance){
  std::istringstream ss(sentance);
  std::string word;
  std::string result;
  while(std::getline(ss, word, ' '))  {
      result += std::toupper(word[0]);
  }
  return result;
}


int main(){
    std::cout << findAbbrivation("Pakistan State Oil") << std::endl;
}
#包括
#包括
#包括
常量标准::字符串查找(常量标准::字符串和句子){
std::istringstream ss(sentance);
字符串字;
std::字符串结果;
while(std::getline(ss,word,')){
结果+=std::toupper(单词[0]);
}
返回结果;
}
int main(){

std::cout几件事。您的WordAbb不够长,无法存储结果,因此您有未定义的行为。其次,您正在设置WordAbb的
i
th字符,而不是正确的第一、第二、第三个字符等。您需要第二个索引。我建议通过不断引用获取参数。您也是prin额外的字符和空格…谢谢!请随时改进答案。