#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char name[10];
char op;
int w, len;
cout<<"Menu\n1.Length of the string(with & without spaces)\n" \\I have edited out the rest as it was irrelevant to my question
cin>>op;
switch(op)
{
case '1': cout<<"Enter a word";
gets(name);
w=strlen(name);
cout<<"The length of the string including spaces is"<<w;
for(int i=0;i!=w;++i)
{
if (name[i]!=' ')
{
len++;
}
}
cout<<"Excluding spaces is\n"<<len;
}
getch();
}
私はこのプログラムにTurbo C7コンパイラを使用しています。 空白を含めて表示しますが、空白を除外しません。 コードが間違っている場合は、修正してください。文字列の長さを検索します(空白は含みません)?
示されたコードには複数のバグがあります。 'gets()'を使わないでください。これは、 'std :: cin'について全く何も知らないCライブラリ関数です。これは未定義の動作です。 [後ろの空白を無視する正しい方法についてあなたのラバーダックに話す](https://en.wikipedia.org/wiki/Rubber_duck_debugging)。 –
コンパイラは 'std :: string'をサポートしていますか? – danielunderwood
私は確信していません。私はそれを一度も使ったことがありません。 –