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

出力は次のとおりです。

Readyaimfire

何が悪いのかわからない、

実行しようとすると、

まったく反応しません。

4

3 に答える 3

2

NULLポインタにアクセスしています。a0 で初期化しました

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
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 に答える
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 に答える