edited by
308 views
0 votes
0 votes

How to take inputs for a string ?

Example:-
 

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define MAX 10000
int main()
{
    char*str;
    int i,test,l;
    scanf("%d",&test);
    for(i=0;i<t;i++)
       {
         str=(char*)malloc(sizeof(char)*MAX);
         fgets(str,MAX,stdin);
         l=strlen(str);
         
         printf("%s",str);
         printf(" %d\n",l);
         free(str);
       }
}



 

this won't work

edited by

2 Answers

0 votes
0 votes

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define MAX 100

int main()
{
    char*str;
    int i,test,l;
    scanf("%d\n",&test);//always give (\n) after scanf when using gets,puts,fgets,fputs
    for(i=0;i<test;i++)//you had made mistake their should be test you had given 't'
       {
         str=(char*)malloc(sizeof(char)*MAX);
         fgets(str,MAX,stdin);
         l=strlen(str);
         
         puts(str);
         printf(" %d\n",l);
         free(str);
       }
       return 0;
}

Run above code it will run.

for run on compiler http://code.geeksforgeeks.org/h1AiYY

Related questions

0 votes
0 votes
0 answers
4