2017-07-26 26 views
-5

考虑两个char数组,“abcdefg”和“xyz”。C或java中的WAP:考虑两个char数组,“abcdefg”和“xyz”...输出必须打印“axbyczdefg”

输出必须打印“axbyczdefg”,请帮助我从早上就一直卡在这。

#include <stdio.h> 
#include <conio.h> 

void main() { 
    int i,j; 
    char a[]="abcdefg"; 
    char b[]="xyz"; 
    for(i=0;i<=6;i++) { 
     printf("%c",a[i]); 
     for(j=0;j<=i;j++) { 
      printf("%c",b[j]); 
     } 
    } 
} 
+0

这个问题的答案在C语言会更理想。 –

+1

请阅读如何问,显示一些代码...东西 https://stackoverflow.com/help/how-to-ask – Aldeguer

+0

*你有什么试过?*提供一些**代码**或任何你已经拥有实现了 – Lino

回答

0

这样

#include <stdio.h> 

int main(void) { 
    const char *a = "abcdefg"; 
    const char *b = "xyz"; 

    while(*a || *b){ 
     if(*a) 
      putchar(*a++); 
     if(*b) 
      putchar(*b++); 
    } 
} 
0

也许这样的事情

@Test 
public void test() { 
    List<String> strings1 = Arrays.asList("a", "b", "c", "d", "e", "f", "g"); 
    List<String> strings2 = Arrays.asList("x", "y", "z"); 

    List<String> result = join(strings1, strings2); 
    System.out.println(String.join(" ", result)); 
} 

List<String> join(List<String> first, List<String> second) { 
    List<String> result = new ArrayList<>(); 

    for (int i = 0; i < first.size(); i++) { 
     result.add(first.get(i)); 

     if (i < second.size()) { 
      result.add(second.get(i)); 
     } 
    } 

    if (second.size() > first.size()) { 
     result.addAll(second.subList(first.size(), second.size())); 
    } 

    return result; 
}