AIM: Write a C program that contains a string (char pointer) with the value ‘Hello World’. The program should XOR each character in this string with 0 and displays the result.
(a) String:
The string is the one-dimensional array of characters terminated by a null ('\0'). Each and every character in the array consumes one byte of memory, and the last character must always be 0. The termination character ('\0') is used to identify where the string ends. In C language string declaration can be done in two ways:
Let's see the example of declaring string by char array in C language.
1. Char ch [9] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd','\0'}; As we know, array index starts from 0, so it will be represented as in the figure given below.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
h | e | l | l | o | w | o | r | l | d | \0 |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char str[]="Hello World";
char str1[11];
int i,len;
len=strlen(str);
for(i=0;i<len;i++)
{
str1[i]=str[i]^0;
printf("%c",str1[i]);
}
printf("\n");
}
Output:
0 Comments