2017-07-26 46 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; 
}