C/Pointer/Pointer String

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Reverse string content using its pointer

  
#include <stdio.h>
#include <string.h>
int main(void)
{
  char str1[] = "Pointers are fun and hard to use";
  char str2[80], *p1, *p2;
  /* make p point to end of str1 */
  p1 = str1 + strlen(str1) - 1;
  p2 = str2;
  while(p1 >= str1)
    *p2++ = *p1--;
  /* null terminate str2 */
  *p2 = "\0";
  printf("%s %s", str1, str2);
  return 0;
}


split an entry of the form Last/First into two parts

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *my_strchr(char *string_ptr, char find){
    while (*string_ptr != find) {
       if (*string_ptr == "\0")
           return (NULL);
       ++string_ptr;
    }
    return (string_ptr);
}
int main()
{
    char line[80]="asdf";
    char *first_ptr;
    char *last_ptr;
    line[strlen(line)] = "\0";
    last_ptr = line;
    first_ptr = my_strchr(line, "d");
    if (first_ptr == NULL) {
        fprintf(stderr,"Error: Unable to find "a" in %s\n", line);
    }
    return (0);
}