注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
- public class Q1_4 {
- public static String replacespaces(char str[],int length){
- int spacecount=0,newlength;
- for(int i=0;i<length;i++){
- if(str[i]==' '){
- spacecount++;
- }
-
-
- }
-
- newlength=length+spacecount*2;
-
- str[newlength]='\0';
-
- for(int i=length-1;i>0;i--){
- if(str[i]==' '){
- str[newlength-1]='0';
- str[newlength-2]='2';
- str[newlength-3]='%';
-
-
-
- }
-
- else{
- str[newlength-1]=str[i];
- newlength=newlength-1;
-
-
- }
-
-
- }
- return String.valueOf(str);
- }
- public static void main(String[] args){
-
- char str[]=new char[100];
- str[0]='a';
- str[1]='b';
- str[2]=' ';
- str[3]='d';
-
- replacespaces(str,100);
-
-
-
- }
-
- }
复制代码 main函数是我自己写的,主要有一下疑问,望大牛指点:
1.第16行的str[newlength]='\0';作用是啥?
2.我这段代码在eclipse运行不了,我自己debug调了,实在解决不了,大牛能否指点一下
|