-4
#include<stdio.h>
#include<string.h>
#include<conio.h>

char *alphabetic (const char *s)
{
    char *a=(void *)0;
    int len,i=0,j=0;
    len=strlen(s);
    for(i=0;i<len;i++)
    {
        if((s[i]>=65))
        {
            if(s[i]<=90)
                {
                    a[j]=s[i];
                    j++;
                }
        }
        else if((s[i]>=97))
        {
            if((s[i]<=122))
            {
                a[j]=s[i];
                j++;
            }
        }
    }
    return (char *)a;
}

int main (void)
{
  char *a, *b, *c;
  a = alphabetic ("Ready... aim... fire!");
  printf ("%s\n", a);


  free(a);
  getch();
  return 0;
}

the output is:

Readyaimfire

I don't know what is wrong,

when i try to run it,

it doesn't respond at all.

4

3 回答 3

2

您正在访问一个NULL指针。a你用 0初始化

char *a=(void *)0;

使用 malloc 分配内存

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;
    }
 }

这将只留下字母字符。

我希望这会对你有所帮助。

于 2013-11-15T03:31:31.753 回答
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;
}
于 2013-11-15T06:07:42.020 回答
0
char *alphabetic (const char *s)
{
    **char *a=(void *)0;**
    int len,i=0,j=0;
    len=strlen(s);
    for(i=0;i<len;i++)
    {

......

指针 a 为 NULL,使用前为其分配内存。例如:

#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;
}
于 2013-11-15T05:35:42.010 回答