• edited by
707 views
0 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

2 Answers

0 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

Position:
Show:

Related questions

0 0 votes
0 0 answers
5.5k
5.5k views
once_2019 asked Oct 7, 2018
5,546 views
Suppose the length of a 10Base5 cable is 2500 m. If the speed of propagation in a thick coaxial cable is 200,000,000 m/s, how long does it take for a bit to travel from t...
2 2 votes
1 answers 1 answer
1.1k
1.1k views
prathams asked Jan 4, 2016
1,112 views
Consider a route in a store and forward network going through 9 intermediate nodes. The packet contains 1100 bits and are transmitted at 64 Kbps. Assume propagation delay...
0 0 votes
0 0 answers
588
588 views