//Program to assign string and display the same
#include<stdio.h> #include<stdlib.h> int main() { char str[]="LearnGood"; printf("Assigned string: %s",str); return 0; }
//specifiying the array string size
#include<stdio.h> #include<stdlib.h> int main() { char str[20]="LearnGood"; printf("Assigned string: %s",str); return 0; }
Assigning string character by character
#include<stdio.h> #include<stdlib.h> int main() { char str[]={'L','e','a','r','n','g','o','o','d','\0'}; printf("\n Assigned string:%s\n",str); return 0; }
//Read a string and display it
#include<stdio.h> #include<stdlib.h> int main() { char str[20]; printf("Enter string: "); scanf("%s",str); printf("\nEntered string: %s\n",str); return 0; }
//It will not allow string
so we need gets(str)
code:
#include<stdio.h> #include<stdlib.h> int main() { char str[20]; printf("Enter string: "); gets(str); printf("\nEntered string: %s\n",str); return 0; }
with gets we can print line with space: Zaki Live
#include<stdio.h> #include<stdlib.h> int main() { char str[20]; int i; printf("entered string:"); //we are not using scanf("") here, it is using getchar and taking input one by one character for(i=0;(str[i]=getchar())!='\n';i++); str[i]='\0'; printf("%s",str); return 0; }
//simple printf
#include<stdio.h> #include<stdlib.h> int main() { char str[20]; int i; printf("Enter string: "); scanf("%s",str); printf("Entered string:%s",str); return 0; }
//using puts
#include<stdio.h> int main(){ char str[20]; int i; printf("enter string:"); scanf("%s",str); printf("entered string is: "); puts(str); return 0; }
//putchar
#include<stdio.h> #include<stdlib.h> int main(){ char str[20]; int i; printf("enter string: "); scanf("%s",str); for(i=0;str[i]!='\0';i++) putchar(str[i]); return 0; }
Program to find length of a given string:
#include<stdio.h> int main() { char sstr[20]; int i; printf("Enter string:"); gets(sstr); for(i=0; sstr[i]!='\0'; i++); printf("length of the string:%d",i); return 0; }
Using string related library function:
#include<stdio.h> #include<string.h> int main() { char str[20]; int i; printf("\nEnter string: "); gets(str); i=strlen(str); printf("%d",i); return 0; }
//string copy with out library function
#include<stdio.h> #include<stdlib.h> int main() { char sourcestr[20],deststr[20]; int i; printf("Enter your string:"); gets(sourcestr); for(i=0; (deststr[i]=sourcestr[i])!='\0';i++); printf("Copied string : %s",deststr); return 0; }
//using strcpy library function
#include<stdio.h> #include<stdlib.h> int main() { char sourcestr[20],deststr[20]; int i; printf("Enter your string:"); gets(sourcestr); strcpy(deststr,sourcestr); printf("Copied string : %s",deststr); return 0; }
Concatenated String:
#include<stdio.h> int main() { char sourcestr1[20],sourcestr2[20]; char deststr[40]; int i,j; printf("here enter 1st string:"); gets(sourcestr1); printf("enter 2nd string:"); gets(sourcestr2); for(i=0; (deststr[i]=sourcestr1[i])!='\0'; i++); for(j=0; (deststr[i]=sourcestr2[j])!='\0'; i++,j++); printf("Concatnated string: %s\n",deststr); return 0; }
Using library function:
#include<stdio.h> int main() { char sourcestr1[20],sourcestr2[20]; char deststr[40]; int i,j; printf("here enter 1st string:"); gets(sourcestr1); printf("enter 2nd string:"); gets(sourcestr2); strcpy(deststr,sourcestr1); strcat(deststr,sourcestr2); printf("Concatanted string: %s\n",deststr); return 0; }