C++程序改错题,程序要求输入一行字符串,统计其中单词个数,单词之间可用一个或多个空格隔开。

C++程序改错题,程序要求输入一行字符串,统计其中单词个数,单词之间可用一个或多个空格隔开。,第1张

问题 c++中最好用string代替char数组 cin>>不是cin<< 此外cin接受输入时遇到空格即结束 所以这样count只会为1 getline会直到\n才结束,第八行最后少了个第九行s[i]=' '应为s[i]==' '这个要牢记

######################我是分隔符#################################

#include <iostream>

#include <string>

using namespace std

void main()

{

string s

int count,i,j

cout<<"输入一行字符串:\n"

getline(cin,s)

for(count=0,j=s.size(),i=0i<j)

{

while(s[i]==' '&&i<j) i++

if(i<j) count++

while(s[i]!= ' '&&i<j) i++

}

cout<<"输入的字符串为:\n"<<s<<endl

cout<<"字符串中包含的单词数为:"<<count<<endl

}

一个三处错误:

import javax.swing.*//下面JOptionPane是这个包里面的。

public class Project0 {

public static String InputWord

public static int sum=0

public static void main(String[] args){

InputWord=typeword()

countword(InputWord)

outputword()

//typeword

//countword

//outputword

}

public static String typeword(){

return(JOptionPane.showInputDialog(null,"Enter a word:"))

}

public static void countword(String word){

word=word.toLowerCase()

for (int i=0i<word.length()i++){ ///不是word.length

if (Character.isLetter(word.charAt(i))){//不是word.CharAt(i),下面同理

if (word.charAt(i)=='x'||word.charAt(i)=='q')

sum+=5

else if (word.charAt(i)=='a'||word.charAt(i)=='e'||word.charAt(i)=='i'||word.charAt(i)=='o'||word.charAt(i)=='u')

sum+=0

else

sum+=1

}

}

}

public static void outputword(){

JOptionPane.showMessageDialog(null,"the word is"+InputWord+"\n"+"Score: "+sum)

}

}

小错不断,并且这只是C不是C++

#include <stdio.h>

#include <string.h>

main()

{ char src[81], dest[81]

int i, j

gets(src)

for(i=j=0*(src+i)!='\0'i++)

{if(*(src+i)!='a')

*(dest+j++)=*(src+i) //

else { *(dest+j++)='S'

*(dest+j++)='H'

*(dest+j++)='U'}}

*(dest+j)='\0' //

puts(dest)//

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11179385.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存