C语言 指针 字符(为什么没有ERROR 但是程序运行不了?怎么改啊?
来源:百度知道 编辑:UC知道 时间:2024/06/03 03:37:52
要求 1将字符串传递给一个函数worl_count( string SIZE)
2使用指针符号(pointer notation)
3为char[100]读取字符( gets(string) for reading a string)
下面是我写的 没检测出ERROR但是 程序无法运行
#include <stdio.h>
#define SIZE 100
void word_count( char *sptr,int size );
int main( void )
{
char string[SIZE];
char *sptr;
*sptr = string;
printf("enter a word:\n");
scanf( "%s", &string[SIZE] );
printf( "count the word:\n" );
word_count( string, SIZE);
return 0;
}
void word_count( char *sptr, int size )
{
int i;
int count;
for( i = 0; i< size; i++){
if( *sptr!='\0'){
count++;
sptr++;
}
}
printf("%s have %d words", *sptr, count);
}
帮帮我哈~~
2使用指针符号(pointer notation)
3为char[100]读取字符( gets(string) for reading a string)
下面是我写的 没检测出ERROR但是 程序无法运行
#include <stdio.h>
#define SIZE 100
void word_count( char *sptr,int size );
int main( void )
{
char string[SIZE];
char *sptr;
*sptr = string;
printf("enter a word:\n");
scanf( "%s", &string[SIZE] );
printf( "count the word:\n" );
word_count( string, SIZE);
return 0;
}
void word_count( char *sptr, int size )
{
int i;
int count;
for( i = 0; i< size; i++){
if( *sptr!='\0'){
count++;
sptr++;
}
}
printf("%s have %d words", *sptr, count);
}
帮帮我哈~~
#include <stdio.h>
#define SIZE 100
void word_count( char *sptr,int size );
int main( void )
{
char string[SIZE];
printf("enter a word:\n");
scanf( "%s", string);
printf( "count the word:\n" );
word_count( string, SIZE);
return 0;
}
void word_count( char *sptr, int size )
{
int i;
int count=0;
for( i = 0; i< size; i++)
{
if( *(sptr+i)!='\0')
{
count++;
}
else break;
}
printf("%s have %d words\n", sptr, count);
}
#include <stdio.h>
#define SIZE 100
void countWord(char *pStr);
main() {
char string[SIZE];
char *pStr;
pStr = string;
printf("Enter your word==>");
scanf("%s",pStr);
countW