Strings C
Strings C
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
You can use the fgets() function to read a line of string. And, you can
use puts() to display the string.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
#include <stdio.h>
void displayString(char str[]);
int main()
{
char str[50];
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output: ");
puts(str);
}
#include<stdio.h>
#include <string.h>
int main()
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
return 0;
}
C Copy String: strcpy()
The strcpy(destination, source) function copies the source string in destination.
#include<stdio.h>
#include <string.h>
int main()
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
int stringLength(char*);
int main()
{
char str[100]={0};
int length;
return 0;
}
/*function definition...*/
int stringLength(char* txt)
{
int i=0,count=0;
while(txt[i++]!='\0'){
count+=1;
}
return count;
}
/********************************************************
* function name :stringCpy
* Parameter :s1,s2 : string
* Description : copies string s2 into s1
********************************************************/
stringCpy(str2,str1);
#include <stdio.h>
/********************************************************
* Description
********************************************************/
int main()
char str[100];
stringLwr(str);
return 0;
int i=0;
while(s[i]!='\0')
s[i]=s[i]+32;
++i;
int i=0;
while(s[i]!='\0')
s[i]=s[i]-32;
++i;