포인터 문자열에서 단어를 알파벳순으로 정렬하는데 생긴 문제좀 봐주세요^^;

mrhyunby의 이미지

char *sentence = "A lot of very useful research has been accumulated";

void sort(int a,int b);
void main()
{
int i=0,n=0;
printf("%s\n",sentence);
while(*(sentence+i)!='\0'){
if(*(sentence+i)==' '){
sort(n,i-1);
n=i;
}
i++;
}
printf("%s\n",sentence);
}

void sort(int a,int b){
int i=0,j=0;
char *temp=" ";
for(i=a;i<=b;i++){
for(j=i;j<=b;j++){
if(*(sentence+i)>*(sentence+j)){
*temp=*(sentence+j);
*(sentence+j)=*(sentence+i);
*(sentence+i)=*temp;
}
}
}
}

이것인데요 문제가 되는 부분이
*temp=*(sentence+j);
*(sentence+j)=*(sentence+i);
*(sentence+i)=*temp;

이부분입니다;;
디버그 하면 여기에 문제있다고 나오는데 고치질 못하겠어요;;
포인터를 빼고 주소끼리 바꾸려고 하면 컴파일 오류나고요.ㅠㅠ

어떻게 바꿔야 하죠??

kewlbear의 이미지

아래와 같이 바꾸면 될 것 같은데 컴파일은 해보지 않았습니다.

/* ... */
char temp;
/* ... */
temp = *(sentence+j);
*(sentence+j) = *(sentence+i);
*(sentence+i) = temp;
/* ... */

dionysos의 이미지

char *sentence = "A lot of very useful research has been accumulated";
코드 영역입니다.

요렇게 바꾸면 될것같은데요
char sentence[] = "A lot of very useful research has been accumulated";
데이터 영역입니다.

노력은 배반하지 않는다.

노력은 배반하지 않는다.

mrhyunby의 이미지

char sentence[] = "A lot of very useful research has been accumulated";
저도 이방법은 생각해봤는데요;;
교수님이 워낙 구닥다리 샘이라.ㅡㅡ ;
정해진 조건에 충실해야한다고;ㅠ
애초에 정해진것은

char *sentence = "A lot of very useful research has been accumulated";
main()
{
}

sort()
{
}
이거밖에없어요;; 그 내용은 알아서
sort에서 정렬되야한다는 것밖에;; 인자 전달되고;;

dionysos의 이미지

주어진 조건을 피할수 있는 방법은 얼마든지 있을거 같은데요

배열을 만들어서 해당 문장을 복사해서 그 문장으로 결과 값을 만드셔도 되고 아니면 Search를 통해 인덱스를 누적하여 출력하는 방법도 있습니다.

------------------------------------
노력은 배반하지 않는다.

노력은 배반하지 않는다.

mrhyunby의 이미지

아 제가 언급을 안했군요;
문제의 수준도 그렇긴 하지만;;
저희가 배운부분만 이용해야해서요;;
책으로 치면
printf,putchar,getchar,scanf,switch 등
반복문
조건문
*
[]
* []
malloc() <--이건 아직 설명을 ;;
strcmp() <--이것도 역시.. 예제에서 나오기만
없네요.ㅠㅠ

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.