我需要输入一个字符串并只返回c中的字母
我不知道怎么了 当我试着运行它时 它根本没有响应。您正在访问一个空指针。您在中用0初始化了一个我需要输入一个字符串并只返回c中的字母,c,C,我不知道怎么了 当我试着运行它时 它根本没有响应。您正在访问一个空指针。您在中用0初始化了一个 Readyaimfire 或者使用malloc分配内存 char *a=(void *)0; 或者将字符串数组传递到函数中 char *a = (char *)malloc(number_of_chars+1); /*1 for NULL character.*/ 并将其用作 char *alphabetic (const char *s, char *a) 或者开发一个就地算法,比如 c
Readyaimfire
或者使用malloc分配内存
char *a=(void *)0;
或者将字符串数组传递到函数中
char *a = (char *)malloc(number_of_chars+1); /*1 for NULL character.*/
并将其用作
char *alphabetic (const char *s, char *a)
或者开发一个就地算法,比如
char a[NUMBEROFCHARS];
alphabetic("Your string..", a);
这将只留下字母字符
我希望这对你有帮助
int j = 0; /*Insertion position.*/
for(i = 0; i<len; ++i)
{
if(isalpha(s[i])) {
s[j] = s[i];
++j;
}
}
指针a为空,请在使用前为其分配内存。例如:
char *alphabetic (const char *s)
{
**char *a=(void *)0;**
int len,i=0,j=0;
len=strlen(s);
for(i=0;i<len;i++)
{
它一直这么说,因为你没有给我们任何关于问题所在的信息。你的问题是什么?你卡在哪里了?我不知道怎么了,当我试着运行它时,它根本没有反应。没有什么不对的
char *alphabetic (const char *s)
{
**char *a=(void *)0;**
int len,i=0,j=0;
len=strlen(s);
for(i=0;i<len;i++)
{
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char *alphabetic (const char *str)
{
int len = strlen(str);
int s_index, tmp_index = 0;
char *tmp = malloc(100);
if (!tmp) {
return NULL;
}
if (!len) {
return NULL;
}
for (s_index = 0; s_index < len; s_index++) {
if (((str[s_index] >= 'a') && (str[s_index] <= 'z')) || \
((str[s_index] >= 'A') && (str[s_index] <= 'Z'))) {
tmp[tmp_index] = str[s_index];
tmp_index++;
}
}
return tmp;
}
int main (void)
{
char *a, *b, *c;
a = alphabetic ("Ready... aim... fire!");
printf ("%s\n", a);
free(a);
getchar();
return 0;
}
#include<stdio.h>
#include<string.h>
#include<ctype.h>
// Return only the alphbets present in the input string
char *alphabetic (const char *s)
{
// No of alphabets present in the input string is unknown at this point.
// So we allocate same number of memory bytes as input string.
char *a=malloc(strlen(s)*sizeof(char));
char *tmp_a=a;
// manipulate till the end of string
while(*s)
{
// If the character is alphabets then copy it to a.
if(isalpha(*s))
{
*tmp_a=*s;
tmp_a++;
}
s++;
}
*tmp_a='\0';
return a;
}
int main (void)
{
char *a, *b, *c;
a=alphabetic("Ready... aim... fire!");
// If the sizeof a is not 0, then the input string has alphabets.
if(strlen(a))
printf ("%s\n", a);
else printf("No Alphabets Present");
free(a);
return 0;
}