将java代码转换为字符串时出错-从函数传输结果
我试图把我写的Java代码转换成C,我刚开始学习C,我把它和Java混淆了 我的java代码:将java代码转换为字符串时出错-从函数传输结果,java,c,Java,C,我试图把我写的Java代码转换成C,我刚开始学习C,我把它和Java混淆了 我的java代码: public class Biggest { private static int testsExecuted = 0; private static int testsFailed = 0; public static void main(String[] args) { System.out.println("Testing typical cases, includ
public class Biggest
{
private static int testsExecuted = 0;
private static int testsFailed = 0;
public static void main(String[] args)
{
System.out.println("Testing typical cases, including punctuation\n");
testLongestWord("the quick brown foxes jumped over the lazy dogs", "jumped");
testLongestWord("hello world she said", "hello");
testLongestWord("Hello\tworld\tshe\tsaid", "Hello");
testLongestWord("HELLO, world she said", "HELLO");
testLongestWord("hello world! she said???", "hello");
testLongestWord("\"hello world!\", she said.", "hello");
testLongestWord("easy as abc123", "abc123");
testLongestWord("easy as abc,123", "easy");
System.out.println("\nTesting empty cases\n");
testLongestWord("", "");
testLongestWord("!", "");
testLongestWord(" ", "");
testLongestWord("\t", "");
testLongestWord(" ", "");
testLongestWord("# $ ? % !", "");
System.out.println("\nTesting edge cases\n");
testLongestWord("a", "a");
testLongestWord("abc", "abc");
testLongestWord("abc d e f ghi", "abc");
testLongestWord("a a b cc dd abc", "abc");
testLongestWord("\"a a b cc dd abc.\"", "abc");
System.out.println("\nTesting apostrophes and dashes\n");
testLongestWord("this isn't five chars", "chars");
testLongestWord("this should've been eight chars said the computer", "should've");
testLongestWord("'this should've been eight chars', said the computer", "should've");
testLongestWord("'hello world!', she said softly.", "softly");
testLongestWord("topsy-turvy is a tenletter word", "topsy-turvy");
testLongestWord("topsy-turvy should not be incorrectly eleven characters", "incorrectly");
testLongestWord("---in-between-these---", "in-between-these");
testLongestWord("---in---between---these---", "between");
testLongestWord("here-is-an-edge-case but a muchmuchlongerword", "muchmuchlongerword");
testLongestWord("d-o-n't-g-o-o-v-e-r-t-h-e-e-d-g-e with muchmuchlongerwords", "muchmuchlongerwords");
testLongestWord("two=five-3 isn't three", "three");
System.out.println("\nThese tests will be opposite in the C version\n");
testLongestWord("the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.", "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch");
testLongestWord("the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.", "antidisestablishment");
testLongestWord("Java strings may contain \0 in the interior", "interior");
testLongestWord("C strings cannot contain \0 in the interior", "strings");
System.out.println("\nTotal number of tests executed: " + testsExecuted);
System.out.println("Number of tests passed: " + (testsExecuted - testsFailed));
System.out.println("Number of tests failed: " + testsFailed);
}
public static void testLongestWord(String line, String expected)
{
String result = longestWord(line);
if (result.equals(expected))
{
System.out.println("Passed: '" + result + "' from '" + line + "'");
}
else
{
System.out.println("FAILED: '" + result + "' instead of '" + expected + "' from '" + line + "'");
testsFailed++;
}
testsExecuted++;
}
public static String longestWord(String line)
{
int pos = 0;
String longest = "";
int longestLength = 0;
String current = "";
int currentLength = 0;
char ch;
line += ' ';
while (pos < line.length())
{
ch = line.charAt(pos);
if ((ch == '\'' || ch == '-') && pos > 0 &&
Character.isLetter(line.charAt(pos - 1)) &&
Character.isLetter(line.charAt(pos + 1)))
{
current += ch;
}
else if (Character.isLetterOrDigit(ch))
{
current += ch;
currentLength++;
}
else
{
if (currentLength > longestLength)
{
longest = current;
longestLength = currentLength;
}
current = "";
currentLength = 0;
}
pos++;
}
return longest;
}
}
公共类
{
私有静态int testsExecuted=0;
私有静态int testsFailed=0;
公共静态void main(字符串[]args)
{
System.out.println(“测试典型案例,包括标点符号”);
testLongestWord(“敏捷的棕色狐狸跳过懒惰的狗”,“跳”);
testLongestWord(“你好,她说的世界”,“你好”);
testLongestWord(“Hello\tworld\tshe\tsaid”,“Hello”);
testLongestWord(“你好,她说的世界”,“你好”);
testLongestWord(“你好,世界!她说了?”,“你好”);
最长的单词(“你好,世界!”,她说,“你好”);
testLongestWord(“简单如abc123”,“abc123”);
testLongestWord(“像abc一样容易,123”,“容易”);
System.out.println(“\n测试空案例\n”);
测试最长的单词(“,”);
测试最长的单词(“!”,“);
测试最长的单词(“,”);
testLongestWord(“\t”,”);
测试最长的单词(“,”);
测试最长单词(“#$?%!”,”);
System.out.println(“\n测试边缘案例\n”);
测试最长单词(“a”、“a”);
测试最长单词(“abc”、“abc”);
testLongestWord(“abc d e f ghi”、“abc”);
testLongestWord(“a a b cc dd abc”、“abc”);
测试最长单词(“a a b cc dd abc.\”,“abc”);
System.out.println(“\n测试撇号和破折号\n”);
testLongestWord(“这不是五个字符”,“字符”);
testLongestWord(“这应该是八个字符,计算机说”,“应该”);
testLongestWord(“这应该是八个字符”,计算机说,“应该”);
最长的单词(“你好,世界!”,她轻声说;
testLongestWord(“topsy-turvy是一个十个字母的单词”,“topsy-turvy”);
testLongestWord(“topsy turvy不应该是错误的十一个字符”,“错误的”);
testLongestWord(“----在这些----”,“在这些之间”);
测试最长的单词(“--in--between--”这些--“,“between”);
testLongestWord(“这是一个边缘案例,但不是muchmuchlongerword”,“muchmuchlongerword”);
testLongestWord(“d-o-n't-g-o-o-v-e-r-t-h-e-e-d-g-e带muchmuchlongerwords”、“muchmuchlongerwords”);
testLongestWord(“二=五-3不是三”,“三”);
System.out.println(“\n这些测试在C版本中将是相反的\n”);
测试最长的词(“反疾病”一词很长,但没有“Llanfairpwlgwyngyllgogerychwyrndrobwyll Llantysiliogogogogoch.”,“Llanfairpwlgwyngyllgogerychwyrndrobyll Llantysiliogogoch.”那么长;
testLongestWord(“反疾病确立主义”一词很长,但没有“Llanfairpwlgwyngyllgogerychwyrndrobwyll Llantysiliogogogogoch”那么长。”,“反疾病确立”);
testLongestWord(“Java字符串内部可能包含\0”,“内部”);
testLongestWord(“C字符串内部不能包含\0”,“字符串”);
System.out.println(“\n执行的测试总数:“+testsExecuted”);
System.out.println(“通过的测试数:”+(testsExecuted-testsFailed));
System.out.println(“失败的测试数:+testsFailed”);
}
公共静态void testLongestWord(字符串行,应为字符串)
{
字符串结果=最长单词(行);
如果(结果等于(预期))
{
System.out.println(“通过:”+result+“'from”“+line+””);
}
其他的
{
System.out.println(“失败:“+result+”,而不是“+line+””中的“+expected+”;
testsFailed++;
}
testsExecuted++;
}
公共静态字符串最长字(字符串行)
{
int pos=0;
字符串最长=”;
int longestLength=0;
字符串current=“”;
int currentLength=0;
char ch;
行+='';
while(位置0和&
字符列表(第1行字符)和
Character.isleter(line.charAt(pos+1)))
{
电流+=ch;
}
else if(字符IsleterOrdigit(ch))
{
电流+=ch;
currentLength++;
}
其他的
{
如果(当前长度>最长长度)
{
最长=电流;
longestLength=当前长度;
}
当前=”;
currentLength=0;
}
pos++;
}
返回时间最长;
}
}
我正在进行的C转换:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
static int testsExecuted = 0;
static int testsFailed = 0;
char testLongestWord(char line[], char expected[]);
void longestWord(char line[]);
int main(int args, char *argv[]){
printf("%s\n", "Testing typical cases, including punctuation\n");
testLongestWord("the quick brown foxes jumped over the lazy dogs", "jumped");
testLongestWord("hello world she said", "hello");
testLongestWord("Hello\tworld\tshe\tsaid", "Hello");
testLongestWord("HELLO, world she said", "HELLO");
testLongestWord("hello world! she said???", "hello");
testLongestWord("\"hello world!\", she said.", "hello");
testLongestWord("easy as abc123", "abc123");
testLongestWord("easy as abc,123", "easy");
printf("\n%s\n", "Testing empty cases\n" );
testLongestWord("", "");
testLongestWord("!", "");
testLongestWord(" ", "");
testLongestWord("\t", "");
testLongestWord(" ", "");
testLongestWord("# $ ? % !", "");
printf("\n%s\n", "Testing edge cases\n" );
testLongestWord("a", "a");
testLongestWord("abc", "abc");
testLongestWord("abc d e f ghi", "abc");
testLongestWord("a a b cc dd abc", "abc");
testLongestWord("\"a a b cc dd abc.\"", "abc");
printf("\n%s\n", "Testing apostrophes and dashes\n" );
testLongestWord("this isn't five chars", "chars");
testLongestWord("this should've been eight chars said the computer", "should've");
testLongestWord("'this should've been eight chars', said the computer", "should've");
testLongestWord("'hello world!', she said softly.", "softly");
testLongestWord("topsy-turvy is a tenletter word", "topsy-turvy");
testLongestWord("topsy-turvy should not be incorrectly eleven characters", "incorrectly");
testLongestWord("---in-between-these---", "in-between-these");
testLongestWord("---in---between---these---", "between");
testLongestWord("here-is-an-edge-case but a muchmuchlongerword", "muchmuchlongerword");
testLongestWord("d-o-n't-g-o-o-v-e-r-t-h-e-e-d-g-e with muchmuchlongerwords", "muchmuchlongerwords");
testLongestWord("two=five-3 isn't three", "three");
printf("\n%s\n", "These tests will be opposite in the C version\n");
testLongestWord("the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.", "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch");
testLongestWord("the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.", "antidisestablishment");
testLongestWord("Java strings may contain \0 in the interior", "interior");
testLongestWord("C strings cannot contain \0 in the interior", "strings");
printf("Total number of test executed: %d\n", testsExecuted );
printf("number of test passed: %d\n", (testsExecuted - testsFailed));
printf("Number of test failed: %d\n", testsFailed );
//longestWord("Java strings may contain \0 in the interior");
}
char testLongestWord(char line[], char expected[]){
char result[200];
/*longestWord(line);*/
strcpy(result, line);
//char *result = longestWord(line);
//printf("%s\n", line );
//int ret = strcmp(result,expected);
if(strcmp(result,expected)){ // function returns 0 if they are equal
printf("passed: '%s' from '%s'\n", result, line);
}else{
printf("FAILED: '%s' from '%s'\n", expected, result);
testsFailed++;
}
testsExecuted++;
return 0;
}
void longestWord(char line[]){
char longest[200];
int pos = 0;
int longestLength = 0;
char current[300];
int currentLength = 0;
char ch;
size_t maxPos = strlen(line);
while(pos < maxPos){
ch = line[pos++];
for(pos = 0; pos < maxPos;pos++){
ch = line[pos++];
if((ch == '\'' || ch == '-') && (pos > 0) && isalpha(line[pos-1]) && isalpha(line[pos+1])){
strcpy(current, &ch);
}else if(isalpha(ch) || isdigit(ch)){
strcpy(current, &ch);
currentLength++;
//printf("%s\n", longest );
}else{
if(currentLength > longestLength){
strcpy(longest,current);
longestLength = currentLength;
}
//strcpy(current, "");
currentLength =0;
}
}
}
}
#包括
#包括
#包括
静态int testsExecuted=0;
静态int testsFailed=0;
字符testLongestWord(字符行[],字符应[]);
空长字(字符行[]);
int main(int args,char*argv[]){
printf(“%s\n”,“测试典型案例,包括标点符号”);
testLongestWord(“敏捷的棕色狐狸跳过懒惰的狗”,“跳”);
testLongestWord(“你好,她说的世界”,“你好”);
testLongestWord(“Hello\tworld\tshe\tsaid”,“Hello”);
testLongestWord(“你好,她说的世界”,“你好”);
testLongestWord(“你好,世界!她说了?”,“你好”);
最长的单词(“你好,世界!”,她说,“你好”);
testLongestWord(“简单如abc123”,“abc123”);
testLongestWord(“像abc一样容易,123”,“容易”);
printf(“\n%s\n”,“测试空案例\n”);
测试最长的单词(“,”);
测试最长的单词(“!”,“);
测试最长的单词(“,”);
testLongestWord(“\t”,”);
测试最长的单词(“,”);
测试最长单词(“#$?%!”,”);
printf(“\n%s\n”,“测试边缘案例\n”);
测试最长单词(“a”、“a”);
测试最长单词(“abc”、“abc”);
testLongestWord(“abc d e f ghi”、“abc”);
testLongestWord(“a a b cc dd abc”、“abc”);
测试最长单词(“a a b cc dd abc.\”,“abc”);
printf(“\n%s\n”,“测试撇号和破折号\n”);
testLongestWord(“这不是五个字符”,“字符”);
testLongestWord(“这应该是八个字符,计算机说”,“应该”);
测试最长的单词(“这应该是八个字符”,c说
char result[] = <something>;
char *result = longestWord(line);
strcat(line, " ");
void longestWord(char line[], char longestBuf[], size_t bufSize)