Hi!
I have written a function for passing a string into it and finding its length.
When I pass a string it prints a correct value.
But when I take two strings and passing them individual len(str1),len(str2) after scanning two strings it returns the wrong value.
In the first case I passed the each string immediately after scanning each of them.
In the second case I scanned the two strings first and passed them then,I got different
answers.Please help me.
First case input and output-
str1=vijay
str2=rajes
output-5,5
Second case input and output-
str1=vijay
str2=rajes
output-4,5
Ist case--
#include<stdio.h>
#include<conio.h>
main(){
int len(char str[]);
char str1[]="",str2[]="";
printf("Enter a string= ");
scanf("%s",str1);
len(str1);
scanf("%s",str2);
len(str2);
getch();
}
int len(char str[]){
int i=0;
while(str[i]!='\0')
++i;
printf("%d",i);
}
2nd--
#include<stdio.h>
#include<conio.h>
main(){
int len(char str[]);
char str1[]="",str2[]="";
printf("Enter a string= ");
scanf("%s",str1);
scanf("%s",str2);
len(str1);
len(str2);
getch();
}
int len(char str[]){
int i=0;
while(str[i]!='\0')
++i;
printf("%d",i);
}
Please help me change this program in order to get right thing,and mention the fault in the program.I guess it is becase of enter(\n) button.
Thanks in advance.