java - 将 java 代码转换为字符串时出错 - 从函数传输结果

标签 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, 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;
  }
}

我正在处理的 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;
            }
        }

    }

}

我收到的错误和警告编辑:我不再有这些错误。

我在java到c中的这行String result = permanentWord(line);方面遇到问题,函数longestWords没有将结果传输到result。有办法做到吗?

最佳答案

在 C 中合并数组和指针有很多错误,我无法指出任何一件事并说如果你改变它,那么一切都会开始工作,甚至你会看到如何继续前进。

您需要退后一步,开始学习 C 概念 - 特别是指针和数组在 C 中如何工作,以及如何在两者之间安全地移动。只有当你真正理解了C如何对待内存,以及如何从基本原理编写出正确的C程序时,你才能开始思考如何将一个可以工作的Java程序转换成一个可以工作的C程序。

尝试此答案以获取更多信息 - Tutorial on C pointers and arrays from a Java standpoint

关于java - 将 java 代码转换为字符串时出错 - 从函数传输结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35113708/

相关文章:

java - 如何让通知在一天中的随机时间出现?

java - 解析纯文本

java - 调用定义中缺少参数错误 : String

c - 测量特定函数的最大和最小执行时间

c++ - 在 C++ 中的 #pragma 注释之前搜索可用的库?

c - C中常量的正则表达式

java - Eclipse 自动生成的 toString() 方法

java - SOCKET 处理并发服务器和客户端通信

c - pthread 互斥体不工作,每次都获取一个随机值

c - 为什么我应该使用双指针变量来接收另一个指针的地址(&ptr1)