make string in c code example
Example 1: string in c
#include <stdio.h>
int main(void) {
char name[] = "Harry Potter";
printf("%c", *name);
printf("%c", *(name+1));
printf("%c", *(name+7));
char *namePtr;
namePtr = name;
printf("%c", *namePtr);
printf("%c", *(namePtr+1));
printf("%c", *(namePtr+7));
}
Example 2: c language string
#include<stdio.h>
#include<conio.h>
void main(){
char string[50];
clrscr();
printf("ENTER STRING:");
gets(s);
printf("TYPED STRING:%s",string);
getch();
}
Example 3: string in c
#include <stdio.h>
void displayString(char str[]);
int main()
{
char str[50];
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
displayString(str);
return 0;
}
void displayString(char str[])
{
printf("String Output: ");
puts(str);
}
Example 4: how to store string in c
char str[4] = "GfG";
Example 5: string in c
#include <stdio.h>
int main() {
char *str1 = strdup("Hello");
char *str2 = malloc(sizeof(char) * (strlen(str1) + 1));
for (int i = 0; i < strlen(str1); ++i)
str2[i] = str1[i];
str2[strlen(str1)] = '\0';
printf("%s --> %s\n", str1, str2);
}