2011-11-16 25 views
0
#include <stdio.h> 
#include <ctype.h> /*header for standard tolower function*/ 
#define STRING_LEN 500 

char * changeCase(char *); 
void stripspaces(char*, char*, char*); 
int isinteger(char*); 
int isHex(char*); 
void strrev(char*); 


int main(void) 
{ 
    char line[STRING_LEN]; 
    char line1[STRING_LEN];  
    char *p1 = line;   
    char *p2 = line; 
    int ok, ok2; 
    printf("Enter a string of up to %d characters:\n", STRING_LEN); 
    while((*p1++ = getchar()) != '\n'); 

    changeCase(line); 
    puts("resulting string is:\n"); 
    printf("%s", line); 
    stripspaces(line, p1, p2); 
    ok = isinteger(line); 
    if (ok) 
    { 
     printf("%s is an integer\n", line); 
    } 
    else 
    { 
     printf("\n%s is NOT an integer\n", line); 
    } 
    ok2 = isHex(line); 
    if (ok2) 
    { 
     printf("\n%s is a hex value \n", line); 
    } 
    else 
    { 
     printf("\n%s is NOT a hex value\n", line); 
    } 

    strrev(line); 

    printf("string is:"); 
    printf("%s", line); 


    getch();  
    getch(); 
    return 0; 
}/*end main*/ 

char * changeCase(char *s){ 

    char *ptr; 
    ptr = s;    /* point to start of string*/ 
    while (*s != '\0')  /*keep going until you reach nul (end of string)*/ 
    { 

      if(!isdigit(*s)) 
      { 

         if(isupper(*s)) 
         { 

           *s = tolower(*s); 

         } 

         else if (islower(*s)) 
         { 

           *s = toupper(*s); 

         } 
         }     
     s++;    /*move to the next character*/ 
    } 
    return ptr;    /* return lowercase version of the string*/ 

}/*end stringToLower*/ 

void stripspaces (char *s, char *x1, char *x2){ 
    *x1 = '\0';     
    x1 = s;     
    while(*x1 != '\0')  
    {  
    if(*x1 == ' ') 
    {      
     ++x1;    
     continue; 
    } 
    else 
     *x2++ = *x1++; 
    } 
    *x2 = '\0';   
    printf("\nWith the spaces removed, the string is now:\n%s\n", s); 
} 

int isinteger(char *s)   /* s is a string */ 
{     /* function returns 1(true) or 0(false) */ 

    if(*s == '+' || '-' || ' '){ 
      s++; 
      } 

    while (*s != '\0') 
    {  

     if (!isdigit(*s)) 
     { 
          return 0; 

          } 

     s++;   /* move to next character */ 
     return 1; 
    } 


    } 
int isHex(char *s) 
{ 

    while (*s != '\0'){ 
    if (!isxdigit(*s)){ 

    return 0; 

         } 
    else { 

    return 1; 

     } s++; } 
} 
void strrev(char *p) 
{ 
int i,j; 
j = strlen(p); 
char temp; 

for (i = 0; i < j; i++) // subscript i goes halfway 
{ 
temp = p[i]; // swap 
p[i] = p[j-1-i]; // opposite 
p[j-1–i] = temp; // elements 
} 
} 

該程序在「p [j-1-i] = temp;交換功能中出現錯誤」錯誤如下「流浪」\ 150「在程序中」想知道我怎麼去解決這個錯誤在程序中,在交換功能中出現「 150」

包含錯誤的函數應該反轉內存中的字符串行,以便在函數已經運行字符串是相反的。不知道這是否真的有效,如果有人也可以指出我在那裏犯的任何錯誤,那將是有用的。

+1

把你的編輯光標在第二個短線,然後按Delete鍵。現在輸入程序員的短劃線(短的一個)。 –

+0

durrr哪個鍵被刪除? –

+1

代碼太多。一般來說,最好準備一個bug的最小工作例子。在這個過程中你可以自己找到答案。 – dmckee

回答

5

仔細看,在第二負在:

p[j-1–i] = temp; // elements 

在我的機器,它看起來是這樣的:。它比-長,所以它顯然是一個不同的字符。減號字符是ASCII 45,而編譯器抱怨字符是ASCII 150

要解決,只需更換與該行:

p[j-1-i] = temp; // elements 
+0

啊!謝謝 :) ! –