README
M-10-STRING-IN-C
- STRING IS CHARACTER TYPE ARRAY
#include <stdio.h>
int main()
{
char a[5];
for (int i = 0; i < 5; i++)
{
scanf("%c", &a[i]);
}
for (int i = 0; i < 5; i++)
{
printf("%c\n", a[i]);
}
return 0;
}
- super power of string is we can take input at once without any loop and print in the same way without any loop using %s
- we can use loop as well.
#include <stdio.h>
int main()
{
char s[10];
scanf("%s", &s);
printf("%s", s);
printf("%c", s[2]); // we can utilize the index as well
return 0;
}
#include <stdio.h>
int main()
{
char s[10];
scanf("%s", &s);
printf("%s", s);
printf("%c", s[2]);
printf("\n%c", s[10]); // will print garbage value because we are taking only 5 inputs
printf("\n%d", s[10]); // will print garbage value because we are taking only 5 inputs
return 0;
}
- how printf in case of string know where to stop printing? when ever all inputs are taken even if the array size in more than the inputs string adds a null character
\0And with the help of this null character it stops taking input. exact end of the inputs taking it sets the null character

#include <stdio.h>
int main()
{
char s[10];
scanf("%s", &s);
printf("%s", s);
printf("%c", s[2]);
printf("\n%c", s[6]);
printf("\n%d", s[6]); // asscii value of the null character will be 0;
printf("\n%c", s[10]); // will print garbage value because we are taking only 5 inputs
printf("\n%d", s[10]); // will print garbage value because we are taking only 5 inputs
return 0;
}
-
we have to take one extra index for string because it will put the null character inside if taking input finishes. i mean we have to take one more index than the input indexes.
-
if we give space included string it breaks when first space comes. its a scanf problem. we will use
gets(),fgets(). but thefgets()will be used more because its standard
#include <stdio.h>
#include<string.h>
int main()
{
char s[50];
// scanf("%s", &s);
// gets(s);
// fgets(s, size, stdin); // size included with extra index for setting null
fgets(s,20, stdin);
printf("%s", s);
return 0;
}
- the problem is when
Shahnawaz SAZID
ami sazid
- if there is input like this entern i mean new line the
fget()stops in the enter and does not print after enter but it takes input the enter. whilegets()does not take the enter input and stops when gets enter. - string initialization with normal procedure
#include <stdio.h>
int main()
{
int a[4] = {10,20,30,40};
char s[5] = {'a','b','c','d','\0'}; // include the null cracter
printf("%s", s);
return 0;
}
- string initialization with super power
#include <stdio.h>
int main()
{
// int a[4] = {10,20,30,40};
// char s[5] = {'a','b','c','d','\0'};
// char s[5] = "abcd";
// char s[5] = "abcd\0"; //we can mention null as well
char s[5] = "abcd e f g h"; // including space initialization
printf("%s", s);
return 0;
}
- find length of string
#include <stdio.h>
int main()
{
char s[101]; // if 100 we have to take 101
scanf("%s",s);
int count = 0;
for(int i =0; s[i] !='\0' ; i++)
{
count++;
}
printf("%s", s);
printf("\n%d", count);
return 0;
}
- find string length built in function
#include <stdio.h>
#include <string.h>
int main()
{
char s[101]; // if 100 we have to take 101
scanf("%s", s);
int sz = strlen(s);
printf("%d", sz);
return 0;
}
#include <stdio.h>
int main()
{
char s[1000001];
fgets(s, 1000001, stdin);
for(int i = 0; s[i] != '\\'; i++){
printf("%c", s[i]);
}
return 0;
}
#include <stdio.h>
# include <string.h>
int main()
{
char s[1000001];
scanf("%s", &s);
int length = strlen(s);
int sum = 0;
for (int i = 0; i < length; i++)
{
sum+=s[i]- '0';
// sum+=s[i]- 48;
}
printf("%d", sum);
return 0;
}