我为我的C班做了另一个练习.代码不会崩溃,但它不能按预期工作.显然我犯了一个我找不到的错误.赋值如下:用户输入两个字符c1和c2以及一个整数n,你必须创建一个动态创建的函数,并返回一个包含n个字符的字符串,如下所示:c1c2c1c2c1c2等.例如:c1 = a和c2 = s和n = 4字符串是:asas
但是我创建的数组不包含c1和c2,而是ASCII表中的一些随机字符.加上这里:
printf("\nThe string is: %s\n",s);
Run Code Online (Sandbox Code Playgroud)
屏幕上的输出是这样的:Ohe string是:I(字符串的Insted是:s-what s is-)这是.exe文件中照片的链接:
#include <stdio.h>
#include <stdlib.h>
char* alternate(char c1,char c2,int n)
{
int i;
char *s;
s=(char*)malloc((n+1)*sizeof(char));
if(s==NULL)
{
puts("Could not allocate memory!");
exit(1);
}
for(i=0;i<n;i++);
{
if(i%2==0)
s[i]=c1;
else
s[i]=c2;
}
s[i]='\0';
return s;
}
main()
{
char c1,c2,*s;
int n;
puts("Give two characters: ");
scanf("%c %c",&c1,&c2);
fflush(stdin);
puts("Give an integer: ");
scanf("%d",&n);
s=alternate(c1,c2,n);
printf("\nThe string is: %s\n",s);
free(s);
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
先感谢您!
我在头文件中遇到了一些代码,我无法弄清楚它想说的是什么:
#define MAP_1 ((unsigned long)0x01)
#define MAP_2 (MAP_1<<1)
#define MAP_3 (MAP_1|MAP_2)
#define MAP_4 (MAP_1<<2)
Run Code Online (Sandbox Code Playgroud)
1)MAP_1是否会被赋予固定值'1'(为什么是十六进制?)?
2)MAP_2是MAP_1 + 1的值吗?
3)MAP_3将是什么?
你好,你可以给我一个javascript函数来替换空格
我用Google搜索,无法让他们工作.我目前正在使用此功能:
function escapeHTMLEncode(str)
{
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
Run Code Online (Sandbox Code Playgroud) 我是Java新手,我正在开发GUI.我面临的问题是我无法在文件处理中使用"readLine()"函数.intellisense没有认识到这种特殊的方法,但我已经看到了这种方法完美运行的代码.我在下面粘贴了我的代码.
try
{
Index ind= new Index();
File file1 = new File(ind.path);
File file2 = new File(file1.getAbsolutePath() + ".tmp");
FileWriter fw1 =new FileWriter(file1,true);
BufferedWriter bf1 = new BufferedWriter(fw1);
BufferedWriter bf2 = new BufferedWriter(new FileWriter(file2));
String line= null;
while((line = bf1.readLine()) != null)
{
String tline = line.trim();
if(tline.contains(inputVal))
{
continue;
}
else
{
bf2.write(line);
}
}
Run Code Online (Sandbox Code Playgroud)
希望有更好的回应.
public class ThreadTest extends Thread
{
int i=0;
public void run()
{
i=1;
}
public static void main(String... args)
{
ThreadTest tTest=new ThreadTest();
tTest.start();
System.out.println(tTest.i);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么这有时打印1,有时打印0?在这个程序中会创建多少个线程?据我了解,这个程序中将创建2个线程.如果我错了,请纠正我.