put together 4 rows of strings and count words

时间:2017-11-13 17:44:43

标签: c string

Error line is: strcpy( tmp, row1+ row2 + row3 + row4 );

I dunno how to put together all rows then count words?

I tried to make function then call it before IF but didn't make it.

Also i thinked about chaning char in int then show it in prinft , also tried to make a array then put all rows in that..

How should i do that?

int main() {

char row1[256];
char row2[256];
char row3[256];
char row4[256];

printf("4 rows of lyric:\n");
fgets(row1,100,stdin);
fgets(row2,100,stdin);
fgets(row3,100,stdin);
fgets(row4,100,stdin);

char *tmp[1000];
strcpy( tmp, row1+ row2 + row3 + row4 );

int count=0;
char *cur= tmp;

for (;;)
{
    while (*cur == ' ')
    {
        *cur++;
    }

    if (*cur == 0)
    {
        break;
    }

    count++;

    while (*cur != 0 && *cur != ' ')
    {
        *cur++;
    }
}

char a1=row1[strlen(row1)-1];
char a2=row1[strlen(row1)-2];
char a3=row1[strlen(row1)-3];

char b1=row2[strlen(row2)-1];
char b2=row2[strlen(row2)-2];
char b3=row2[strlen(row2)-3];

char c1=row3[strlen(row3)-1];
char c2=row3[strlen(row3)-2];
char c3=row3[strlen(row3)-3];

char d1=row4[strlen(row4)-1];
char d2=row4[strlen(row4)-2];
char d3=row4[strlen(row4)-3];

    if( a1==d1 &&
     a2==d2 &&
     a3==d3 &&
     b1==c1 &&
     b2==c2 &&
     b3==c3 ) {

            printf("1 = 4 and 2 = 3, number of words %c." cur);

}
else if ( a1==c1 &&
          a2==c2 &&
          a3==c3 &&
          b1==d1 &&
          b2==d2 &&
          b3==d3 ) {

            printf("1 = 3 and 2 = 4, number of words %c.", cur);

}
else if ( a1==b1 &&
          a2==b2 &&
          a3==b3 &&
          c1==d1 &&
          c2==d2 &&
          c3==d3 ) {

            printf("1 = 2 and 3 = 4, number of words %c.",cur);

}

else {

        printf("Nijedna rima nije pronadjena u strofi!");

}


return 0;

}

1 个答案:

答案 0 :(得分:1)

 char * stpcpy(char * restrict dst, const char * restrict src);

您传递的参数不是char*类型。

此外,您无法在c中使用+连接字符串。使用strcatstrncat

作为对用户评论的回复: -

你应该记住三件事

  • 第一个参数应该是可修改的。
  • 第一个参数有足够的空间来容纳组合字符串。
  • 第一个和第二个参数之间没有重叠。

考虑到这三条规则,可以使用这些规则。