|
查看: 958|回复: 13
|
C Language: String [已解决]
[复制链接]
|
|
|
- /*
- 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.
- ***************************************************************************************
- e.g.
- Enter two strings: string1 string2
- The result of comparison is : ???
- The value of string1 is :
- The number of vowel in string1
- a =??
- e =??
- i =??
- o =??
- u =??
- ****************************************************************************************
- ?? : are based on the value that u gave to the variables
- */
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char string1[20],string2[20],string3[50];
- int a=0,e=0,i=0,o=0,u=0;
- printf("Enter two strings: \n";
- gets(string1);
- gets(string2);
- string3=strcat(string1,string2);
- printf("The result of comparison is: %d\n",strcmp(string1,string2));
- printf("The value of string1 is: %s\n",string3);
- for(i=0;i<20;i++)
- {
- if (string1=='a'||string1=='A')
- a=a+1;
- if (string1=='e'||string1=='E')
- e=e+1;
- if (string1=='i'||string1=='I')
- i=i+1;
- if (string1=='o'||string1=='O')
- o=o+1;
- if (string1=='u'||string1=='U')
- u=u+1;
- }
- printf("The number of vowel in string1:\n";
- printf("a=%d\n",a);
- printf("e=%d\n",e);
- printf("i=%d\n",i);
- printf("o=%d\n",o);
- printf("u=%d\n",u);
- }
- /*
复制代码
(36) : error C2106: '=' : left operand must be l-value
怎么debug?
run 不到。。。
[ 本帖最后由 蜡笔小烦 于 18-10-2008 03:26 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 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还是有问题。。。
-
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char string1[20],string2[20],string3[50];
- int count,a=0,e=0,i=0,o=0,u=0;
- printf("Enter a string as string1: ");
- gets(string1);
- printf("Enter a string as string2: ");
- gets(string2);
- strcpy(string3, string1);
- strcat(string3,string2);
- printf("The result of comparison is: %d\n",strcmp(string1,string2));
- printf("The value of string1 is: %s\n",string3);
- for(count=0;count<20;count++)
- {
- if (string1[count]=='a'||string1[count]=='A')
- a=a+1;
- if (string1[count]=='e'||string1[count]=='E')
- e=e+1;
- if (string1[count]=='i'||string1[count]=='I')
- i=i+1;
- if (string1[count]=='o'||string1[count]=='O')
- o=o+1;
- if (string1[count]=='u'||string1[count]=='U')
- u=u+1;
- }
- printf("The number of vowel in string1:\n");
- printf("a=%d\n",a);
- printf("e=%d\n",e);
- printf("i=%d\n",i);
- printf("o=%d\n",o);
- printf("u=%d\n",u);
- }
复制代码
[ 本帖最后由 蜡笔小烦 于 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
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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
|
显示全部楼层
谢谢你,明白了终于。。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|