C Tutorial/String/String Terminator

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

By setting first[4] to NULL ("\0"), we can shorten the string

#include <string.h>
    #include <stdio.h>
    int main()
    {
        char name[30];
        strcpy(name, "Saaaaaaaaaaaaaaam");
        printf("The name is %s\n", name);
        name[4] = "\0";
        printf("The name is %s\n", name);
        return (0);
    }
The name is Saaaaaaaaaaaaaaam
The name is Saaa

Displaying a string with a string terminator in the middle

#include <stdio.h>
int main(void)
{
  printf("The character \0 is used to terminate a string.");
  return 0;
}
The character

Overwrite the newline character in string

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
  char text[100];                
  printf("\nEnter the string to be searched(less than 100 characters):\n");
  fgets(text, sizeof(text), stdin);
  text[strlen(text)-1] = "\0";
  printf("%s",text);
}
Enter the string to be searched(less than 100 characters):
     string
     string