2013-11-15 38 views
-3
#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; 
} 

輸出爲:我需要輸入一個字符串,並在C只返回字母

Readyaimfire 

我不知道什麼是錯的,

當我嘗試運行它,

它劑量反應。

在此先感謝。

它看起來像你的文章主要是代碼; 請添加更多的細節。

它爲什麼一直說:P

+2

口口聲聲說是因爲你沒有給我們任何信息的問題是什麼。你的問題是什麼?你卡在哪裏? – user1118321

+0

'我不知道什麼是錯的, 當我嘗試運行它, 它劑量反應。 ' 沒有錯 – GNKeshava

回答

2

您正在訪問NULL指針。您在

char *a=(void *)0; 

要麼初始化爲0 a使用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; 
    } 
} 

這將只留下字母字符。

我希望這會幫助你。

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++) 
    { 

.............

指針爲NULL,使用前存儲器的alloc它。例如:

#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; 
} 
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; 
} 
相關問題