在scanf中格式 %S需要类型 Char ,但实参 2的来型为 char ()[10]怎么解决
时间: 2024-02-06 10:11:26 浏览: 17
在C语言中,%S格式符是用来读取宽字符字符串的,对应的实参类型应该是wchar_t*。而char*类型的字符串使用%s格式符进行读取。因此,如果实参2的类型为char() [10],表示一个字符数组,那么可以使用%s格式符来读取它。如果需要读取宽字符字符串,可以将实参2的类型改为wchar_t*类型的指针。例如:
```
char str[10];
scanf("%s", str); // 使用%s格式符读取字符数组
wchar_t wstr[10];
scanf("%ls", wstr); // 使用%ls格式符读取宽字符字符串
```
相关问题
2. 编写一个程序,其中用户自定义函数strcompare函数实现两个字符串s1,s2的比较,函数原型声明为:int strcompare( char *str1 , char *str2 ) ; 分别有如下要求: (1)调用函数时实参用数组名,函数形参用指针变量。 (2)调用函数时实参和函数形参都用指针变量。 (3)调用函数时实参用指针变量,函数形参用数组名。
方案1:
```c
#include <stdio.h>
#include <string.h>
int strcompare(char *str1, char *str2);
int main() {
char s1[100], s2[100];
printf("请输入两个字符串:\n");
scanf("%s%s", s1, s2);
int result = strcompare(s1, s2);
if(result > 0) {
printf("字符串%s大于字符串%s\n", s1, s2);
} else if(result < 0) {
printf("字符串%s小于字符串%s\n", s1, s2);
} else {
printf("字符串%s等于字符串%s\n", s1, s2);
}
return 0;
}
int strcompare(char *str1, char *str2) {
return strcmp(str1, str2);
}
```
方案2:
```c
#include <stdio.h>
#include <string.h>
int strcompare(char *str1, char *str2);
int main() {
char s1[100], s2[100];
printf("请输入两个字符串:\n");
scanf("%s%s", s1, s2);
int result = strcompare(s1, s2);
if(result > 0) {
printf("字符串%s大于字符串%s\n", s1, s2);
} else if(result < 0) {
printf("字符串%s小于字符串%s\n", s1, s2);
} else {
printf("字符串%s等于字符串%s\n", s1, s2);
}
return 0;
}
int strcompare(char *str1, char *str2) {
int i = 0;
while(str1[i] && str2[i] && str1[i] == str2[i]) {
i++;
}
return str1[i] - str2[i];
}
```
方案3:
```c
#include <stdio.h>
#include <string.h>
int strcompare(char str1[], char str2[]);
int main() {
char s1[100], s2[100];
printf("请输入两个字符串:\n");
scanf("%s%s", s1, s2);
int result = strcompare(s1, s2);
if(result > 0) {
printf("字符串%s大于字符串%s\n", s1, s2);
} else if(result < 0) {
printf("字符串%s小于字符串%s\n", s1, s2);
} else {
printf("字符串%s等于字符串%s\n", s1, s2);
}
return 0;
}
int strcompare(char str1[], char str2[]) {
int i = 0;
while(str1[i] && str2[i] && str1[i] == str2[i]) {
i++;
}
return str1[i] - str2[i];
}
```
编写一个比较两个字符串大小的函数int MyStrComp(char* s1,char* s2),要求s1小于s2时函数返回-1,s1大于s2返回1,s1等于s2返回0;主函数中定义两个字符数组char* a[100],char* b[100],然后从键盘获取两个任意字符串赋值给两个字符数组,并打印输出两字符数组;接着调用int MyStrComp(char* s1,char* s2),将数组名作为实参,根据函数的返回值,在屏幕输出字符串比较结果,用c语言编写
好的,以下是C语言的代码实现:
```c
#include <stdio.h>
int MyStrComp(char* s1, char* s2) {
int i = 0;
while (s1[i] != '\0' && s2[i] != '\0') {
if (s1[i] < s2[i]) {
return -1;
} else if (s1[i] > s2[i]) {
return 1;
}
i++;
}
if (s1[i] == '\0' && s2[i] == '\0') {
return 0;
} else if (s1[i] == '\0') {
return -1;
} else {
return 1;
}
}
int main() {
char* a[100];
char* b[100];
printf("请输入字符串a:");
scanf("%s", a);
printf("请输入字符串b:");
scanf("%s", b);
printf("输入的字符串a为:%s\n", a);
printf("输入的字符串b为:%s\n", b);
int res = MyStrComp(a, b);
if (res == -1) {
printf("a小于b\n");
} else if (res == 0) {
printf("a等于b\n");
} else {
printf("a大于b\n");
}
return 0;
}
```
在上面的代码中,我们首先定义了一个`MyStrComp`函数,该函数接收两个字符指针`s1`和`s2`作为参数。函数中采用了逐个比较字符的方法,如果`s1`小于`s2`,则返回-1;如果`s1`大于`s2`,则返回1;如果`s1`等于`s2`,则返回0。同时,如果两个字符串的长度不一致,且其中一个字符串已经比较完了,那么我们就认为它比另一个字符串小。
在`main`函数中,我们首先定义了两个字符指针数组`a`和`b`,然后通过`scanf`函数从键盘获取两个字符串,并打印输出。接着,我们调用了`MyStrComp`函数,并根据函数的返回值打印出字符串的比较结果。
注意,上述代码只适用于字符串长度不超过100的情况。如果需要处理更长的字符串,需要修改代码中的数组长度。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)