我有一個用短劃線替換空格的程序。現在我需要能夠計算已被替換的空間量並將其打印出來。這裏是我的間距替換編碼。向程序添加空間計數器
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char string[100], *space;
{
printf("Enter a string here: \n"); //Enter a string in command prompt
fgets(string, sizeof(string), stdin); //scans it and places it into a string
space = string;
while (*space == ' '? (*space = '-'): *space++);
printf("%s\n", string);
}
getchar();
}
這是計算空間數的代碼。
#include <iostream>
#include <string>
int count(const std::string& input)
{
int iSpaces = 0;
for (int i = 0; i < input.size(); ++i)
if (input[i] == ' ') ++iSpaces;
return iSpaces;
}
int main()
{
std::string input;
std::cout << "Enter text: ";
std::getline(std::cin, input);
int numSpaces = count(input);
std::cout << "Number of spaces: " << numSpaces << std::endl;
std::cin.ignore();
return 0;
}
我不知道如何把2結合在一起?誰能幫忙?
UPDATE:
我已經改變了我的代碼如下:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int numSpaces = 0;
int main()
{
char string[100], *space;
{
printf("Enter a string here: \n"); //Enter a string in command prompt
fgets(string, sizeof(string), stdin); //scans it and places it into a string
space = string;
while (*space == ' '? (*space = '-'): *space++);
printf("%s\n", string);
}
while (*space)
{
if(*space == ' ')
{
*space = '-';
++numSpaces;
}
++space;
printf("%f\n", numSpaces);
}
getchar();
}
問題的結果。我不斷收到負載零
搞笑的是你寫的C代碼的一個組成部分,在C++爲其他。 – devnull
確實如此:D – Joze
*從一個人複製C代碼,從另一個人複製C++代碼? – benjymous