0
C string length without using strlen()
#include <stdio.h> int main() { char s[10]; int i; printf("Enter a string: "); scanf("%s", s); for(i = 0; s[i] != '\0'; ++i); printf("Length of string: %d", i); return 0; } If i/p: “ i a m” I want o/p: 6 How can i modify it
12 odpowiedzi
+ 4
The issue your having is with the scanf...If you really need to use it try this (Honfu posted this method a few months back).:-
#include <stdio.h>
int main(){
char s[10];
printf("Enter a string: ");
scanf("%[^\n]s", s);
int i = 0;
while(s[i])
i++;
printf("Length of string: %d", i);
return 0;
}
+ 4
Haritha 79
Try this code it's working properly but shows some warning ignore that.👍👍
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char s[10];
int i;
printf("Enter a string: ");
gets(s);
puts(s);
for(i=0; s[i]; i++);
printf("Length of string: %d", i);
return 0;
}
+ 1
! Bad Coder
thnq😀
0
Modify it how? What exactly is your problem/question?
0
Create an int counter, in for loop increase it by 1. After for loop is complete add 1 again for null terminator.
0
loop condition??
0
how to add 1??
i didnt understand
why to add again
0
can u send u code
if there is no condition then for loop repeats?
0
Mustafa K. thnk you i got it😊
0
Haritha 79 For loop repeats till the last index of string after that it breaks.
0
Haritha 79 ur welcome 👍