1.2 Reverse String

1.2 Implement a function void reverse(char *str) in C or C ++ which reverses a null-terminated string.


1. null-terminated string

A null-terminated string is a character string stored as an array containing the characters and terminated with a null character '\0'. 

Alternative names are C string, which refers to the C programming language and ASCIIZ.

The length of a C string is found by searching for the NUL byte. This can be slow as it takes O(n) linear time with respect to the string leght.

It also means that a NUL cannot be inside the string, as the only NUL is the one marking the end.   (Wikipedia)


2. bus error

It normally means an un-aligned access. An attempt to access memory that isn't physically present would give a bus error. But you won't see this if you are using a processor with an MMU and an OS that's not buggy, because you won't hava any non-existent memory mapped to your process's address space.

A bus error rarely means that the computer hardware is physically broken, it is normally caused by a bug in a program's source code.

Bus errors are usually signaled with the SIGBUS signal, but SIGBUS can also be caused by any general device fault that the computer detects. A bus error rarely means that the computer hardware is physically broken—it is normally caused by a bug in a program's source code. (Wikipedia)

There are two main causes of bus errors:

non-existent address 

The CPU is instructed by software to read or write a specific physical memory address. Accordingly, the CPU sets this physical address on its address bus and requests all other hardware connected to the CPU to respond with the results, if they answer for this specific address. If no other hardware responds, the CPU raises an exception, stating that the requested physical address is unrecognized by the whole computer system. Note that this only covers physical memory addresses. Trying to access an undefined virtual memory address is generally considered to be a segmentation fault rather than a bus error, though if the MMU is separate, the processor can't tell the difference.
unaligned access 
Most CPUs are byte-addressable, where each unique memory address refers to an 8-bit byte. Most CPUs can access individual bytes from each memory address, but they generally cannot access larger units (16 bits, 32 bits, 64 bits and so on) without these units being "aligned" to a specific boundary. For example, if multi-byte accesses must be 16 bit-aligned, addresses (given in bytes) at 0, 2, 4, and so on would be considered aligned and therefore accessible, while addresses 1, 3, 5, and so on would be considered unaligned. Similarly, if multi-byte accesses must be 32-bit aligned, addresses 0, 4, 8, 12, and so on would be considered aligned and therefore accessible, and all addresses in between would be considered unaligned. Attempting to access a unit larger than a byte at an unaligned address can cause a bus error.

CPUs generally access data at the full width of their data bus at all times. To address bytes, they access memory at the full width of their data bus, then mask and shift to address the individual byte. This is inefficient, but tolerated as it is an essential feature for most software, especially string processing. Unlike bytes, larger units can span two aligned addresses and would thus require more than one fetch on the data bus. It is possible for CPUs to support this, but this functionality is rarely required directly at the machine code level, thus CPU designers normally avoid implementing it and instead issue bus errors for unaligned memory access.


3.  const point and point const

There is a problem when calling the reverse function.

int main() {
 char * p = strdup("abcde");
 /* char * p = “abcde”; */ // bus error
 printf("The original string is:\n%s\n", p);
 reverse(p);
 printf("After reverse:\n%s\n", p);
 return 0;
}

When declare a constant string like that, you can't change the direction it points to.

Here is what I've got from StackOverFlow:

const char * andchar const*have the same meaning: the pointed value is const, but the pointer itself can be changed to another address.

char * const: the pointed value can be changed, but the pointer itself is const. It can't be modified to point to another address. 

After initialization, *p = 'X'; is valid and p=&X; is not.

If the * appears at last, it means the string itself can't be changed. Otherwise, it means the pointer points to the memory location can't be changed.


4. strdup in C

char *strdup (const char *s);

This function returns a pointer to a new string which is a duplicate of the string s or NULL if insufficient memory was available. Memory for the new string is obtained with malloc(3) and can be freed with free(3).


C

void reverse(char *str){
  char* end = str;
  char temp;
  if (str){
    while (*end){ // find the end of the string
      ++end;
    }
    --end; // set one char back, since last char is null
    while(str < end){  // swap characters from start with the end, until 
      temp = *str;     // pointers meet in middle
      *str++ = *end;
      *end-- = temp;
    }
  }
}


JAVA

class Reverse{
  public static void main(String[] args){
    String str = "abababa";
    reverse(str);
  }
  
  public static void reverse(String str){
    char[] rev = new char[str.length()];
    for (int i=str.length();i>0;i--){
      rev[str.length()-i] = str.charAt(i-1);
      System.out.print(rev[str.length()-i]);
    }
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值