十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
#include"iostream.h"
目前创新互联已为上1000家的企业提供了网站建设、域名、网页空间、网站托管维护、企业网站设计、连城网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
#include"stdio.h"
int num_str(char str[])
{
int num= 0;
for(int i= 0; str[i]!='\0'; i++)
if(str[i]= 'a' str[i]= 'z' )
num++;
return num;
}
void main()
{
char s[100];
puts("请输入字符串:");
gets(s);
printf("%d\n", num_str(s));
}
在C语言中,数组作为参数,进行传递时,传递的是指针 ,换句话说,字符数组作为参数,进行传递时,传递的是字符数组的起始地址,相当于一个字符指针,两者没有区别。比如下面的代码:
#include stdio.h
void f1(char *s) { printf("%s\n", s);}
void f2(char sa[]) { printf("%s\n", sa);}
int main()
{
char rsa[] ="world hello";
f1(rsa);
f2(rsa);
return 0;
}
#include stdio.h
/*比较两个字符串*/
/*src、dst相等 return 1*/
/*src比dst短 return -2*/
/*src比dst长 return 2*/
/*src、dst长度一致但不相等 return -1*/
int stringCmp (char *src, char *dst) {
if (strlen (src) strlen (dst))
return -2;
else if (strlen (src) strlen (dst))
return 2;
else {
while (*src *dst) {
if (*src != *dst)
return -1;
src++;
dst++;
}
return 1;
}
}
/*合并两个字符串*/
void mergeString(char* str1, char* str2, char* merge) {
while (*merge++ = *str1++); /*复制str1并将指针移动到字符串的末尾的下一个位置*/
merge--; /*退到字符串的末尾*/
while (*merge++ = *str2++); /*从末尾(\0)的位置开始将str2拷贝进来*/
*merge = '\0';
}
int main(void) {
char *str1_1="hel",*str1_2="lo", *merge1;
char *str2_1="ness",*str2_2="happy", *merge2;
char *str3_1="hi",*str3_2="hi", *merge3;
char *str4_1="hello",*str4_2="world", *merge4;
puts ("第1组字符串:");
puts (str1_1);
puts (str1_2);
if (stringCmp (str1_1, str1_2) == 1)
puts (str1_1);
else if (stringCmp (str1_1, str1_2) == -2) {
mergeString (str1_2, str1_1, merge1);
puts (merge1);
}
else if (stringCmp (str1_1, str1_2) == 2 || stringCmp (str1_1, str1_2) == -1) {
mergeString (str1_1, str1_2, merge1);
puts (merge1);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第2组字符串:");
puts (str2_1);
puts (str2_2);
if (stringCmp (str2_1, str2_2) == 1)
puts (str2_1);
else if (stringCmp (str2_1, str2_2) == -2) {
mergeString (str2_2, str2_1, merge2);
puts (merge2);
}
else if (stringCmp (str2_1, str2_2) == 2 || stringCmp (str2_1, str2_2) == -1) {
mergeString (str2_1, str2_2, merge2);
puts (merge2);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第3组字符串:");
puts (str3_1);
puts (str3_2);
if (stringCmp (str3_1, str3_2) == 1)
puts (str3_1);
else if (stringCmp (str3_1, str3_2) == -2) {
mergeString (str3_2, str3_1, merge3);
puts (merge3);
}
else if (stringCmp (str3_1, str3_2) == 2 || stringCmp (str3_1, str3_2) == -1) {
mergeString (str3_1, str3_2, merge3);
puts (merge3);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第4组字符串:");
puts (str4_1);
puts (str4_2);
if (stringCmp (str4_1, str4_2) == 1)
puts (str4_1);
else if (stringCmp (str4_1, str4_2) == -2) {
mergeString (str4_2, str4_1, merge4);
puts (merge4);
}
else if (stringCmp (str4_1, str4_2) == 2 || stringCmp (str4_1, str4_2) == -1) {
mergeString (str4_1, str4_2, merge4);
puts (merge4);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
getch (); /*屏幕暂留*/
return 0;
}
运行结果
不可能达到目的。因为C语言中没有引用这类型,C++才有。另,C/C++都不支持传递数组,只能传递数组的首元素或行指针,所以即使是C++,也只能传递数组指针的引用,而不可能传递数组的引用,因为数组没有引用。