佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 958|回复: 13

C Language: String [已解决]

[复制链接]
发表于 15-10-2008 06:00 PM | 显示全部楼层 |阅读模式
  1. /*
  2. Write a C program that prompt user for two strings [ie: string1, string2].
  3. Compare the two strings using string compare function (refer String Handling Library).
  4. Print the comparison output. Then, append the string2 to the string1. Print string1.
  5. Lastly, calculate the number of vowel in string1.

  6. ***************************************************************************************
  7. e.g.

  8. Enter two strings: string1 string2
  9. The result of comparison is : ???
  10. The value of string1 is :
  11. The number of vowel in string1
  12. a =??
  13. e =??
  14. i =??
  15. o =??
  16. u =??

  17. ****************************************************************************************
  18. ?? : are based on the value that u gave to the variables
  19. */

  20. #include<stdio.h>
  21. #include<string.h>

  22. int main()
  23. {
  24. char string1[20],string2[20],string3[50];
  25. int a=0,e=0,i=0,o=0,u=0;

  26. printf("Enter two strings: \n";
  27. gets(string1);
  28. gets(string2);
  29. string3=strcat(string1,string2);
  30. printf("The result of comparison is: %d\n",strcmp(string1,string2));
  31. printf("The value of string1 is: %s\n",string3);

  32. for(i=0;i<20;i++)
  33. {
  34. if (string1=='a'||string1=='A')
  35. a=a+1;
  36. if (string1=='e'||string1=='E')
  37. e=e+1;
  38. if (string1=='i'||string1=='I')
  39. i=i+1;
  40. if (string1=='o'||string1=='O')
  41. o=o+1;
  42. if (string1=='u'||string1=='U')
  43. u=u+1;
  44. }

  45. printf("The number of vowel in string1:\n";
  46. printf("a=%d\n",a);
  47. printf("e=%d\n",e);
  48. printf("i=%d\n",i);
  49. printf("o=%d\n",o);
  50. printf("u=%d\n",u);

  51. }
  52. /*
复制代码

(36) : error C2106: '=' : left operand must be l-value


怎么debug?
run 不到。。。

[ 本帖最后由 蜡笔小烦 于 18-10-2008 03:26 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 15-10-2008 07:26 PM | 显示全部楼层
strcat 的 return value 不是 string
應該是 destination 的 address來的...
改成:
strcpy(string3, string1);
strcat(string3,string2);

還有就是你的 i 有衝突...

[ 本帖最后由 cheng1986 于 15-10-2008 07:31 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 15-10-2008 08:20 PM | 显示全部楼层
原帖由 cheng1986 于 15-10-2008 07:26 PM 发表
strcat 的 return value 不是 string
應該是 destination 的 address來的...
改成:
strcpy(string3, string1);
strcat(string3,string2);

還有就是你的 i 有衝突...


谢谢
error没有了
我把i 换成count了
可是output还是有问题。。。


  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5. char string1[20],string2[20],string3[50];
  6. int count,a=0,e=0,i=0,o=0,u=0;
  7. printf("Enter a string as string1: ");
  8. gets(string1);
  9. printf("Enter a string as string2: ");
  10. gets(string2);
  11. strcpy(string3, string1);
  12. strcat(string3,string2);
  13. printf("The result of comparison is: %d\n",strcmp(string1,string2));
  14. printf("The value of string1 is: %s\n",string3);
  15. for(count=0;count<20;count++)
  16. {
  17.   if (string1[count]=='a'||string1[count]=='A')
  18.    a=a+1;
  19.   if (string1[count]=='e'||string1[count]=='E')
  20.    e=e+1;
  21.   if (string1[count]=='i'||string1[count]=='I')
  22.    i=i+1;
  23.   if (string1[count]=='o'||string1[count]=='O')
  24.    o=o+1;
  25.   if (string1[count]=='u'||string1[count]=='U')
  26.    u=u+1;
  27. }
  28. printf("The number of vowel in string1:\n");
  29. printf("a=%d\n",a);
  30. printf("e=%d\n",e);
  31. printf("i=%d\n",i);
  32. printf("o=%d\n",o);
  33. printf("u=%d\n",u);
  34. }
复制代码

[ 本帖最后由 蜡笔小烦 于 15-10-2008 08:24 PM 编辑 ]
回复

使用道具 举报

发表于 15-10-2008 09:03 PM | 显示全部楼层
你的output有问题是什么问题? 问问题之前, 麻烦你用脑一下, 把你的问题写得一清二楚。
回复

使用道具 举报

 楼主| 发表于 15-10-2008 09:20 PM | 显示全部楼层
原帖由 糯米鸡 于 15-10-2008 09:03 PM 发表
你的output有问题是什么问题? 问问题之前, 麻烦你用脑一下, 把你的问题写得一清二楚。


Enter a string as string1: saya
Enter a string as string1: dia
The result of comparison is: 1
The value of string1 is: sayadia
The number of vowel in string1:
a=2
e=0
i=0
o=0
u=0
回复

使用道具 举报

发表于 15-10-2008 09:29 PM | 显示全部楼层
有什么问题?
回复

使用道具 举报

Follow Us
 楼主| 发表于 15-10-2008 09:37 PM | 显示全部楼层
Write a C program that prompt user for two strings [ie: string1, string2].
Compare the two strings using string compare function (refer String Handling Library).
Print the comparison output. Then, append the string2 to the string1. Print string1.
Lastly, calculate the number of vowel in string1.


题目这样子的话,
我在想可不可以用string3了呢?
因为它要我append string2去string1然后要print string1
最后还是要算出string1的aeiou的数量。。。
回复

使用道具 举报

发表于 15-10-2008 09:41 PM | 显示全部楼层
可以把string3去掉

先compare string1 和 string2

再strcat(string1, string2);

P.S. string1的size要比較大

[ 本帖最后由 零翼 于 15-10-2008 09:42 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 15-10-2008 09:51 PM | 显示全部楼层
原帖由 零翼 于 15-10-2008 09:41 PM 发表
可以把string3去掉

先compare string1 和 string2

再strcat(string1, string2);

P.S. string1的size要比較大


可以了
没问题了
谢谢
回复

使用道具 举报

发表于 16-10-2008 06:27 PM | 显示全部楼层
借楼主的楼来问个问题:
为什么我用strncmp的时候,不用include<cstring>也可以照常运作的?
我只是include<iostream>而已。
回复

使用道具 举报

发表于 17-10-2008 09:08 AM | 显示全部楼层

回复 10# Gvr 的帖子

gcc / mingw 会自动搜索有关的library ( 只是 ANSI standard library 没错的话 )然后加入。
回复

使用道具 举报

发表于 18-10-2008 02:45 AM | 显示全部楼层
所以一下这些也可以不用include吗?
18 Legacy C Library Headers>> cassert | cctype | cerrno | cfloat | ciso646 | climits | clocale | cmath | csetjmp | csignal | cstdarg | cstddef | cstdio | cstdlib | cstring | ctime | cwchar | cwctype
回复

使用道具 举报

发表于 18-10-2008 08:34 AM | 显示全部楼层

回复 12# Gvr 的帖子

最好insert, 毕竟是standard写法。 若没insert header file, 在另一个compiler上就不能compile了。
回复

使用道具 举报

发表于 18-10-2008 02:28 PM | 显示全部楼层
谢谢你,明白了终于。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 11:26 AM , Processed in 0.150451 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表