Problem with substring program

hello friends,

I wrote a c program to perform the substring operation on the string str[100]. This program was to take the position index pos and the number of characters len that needs to be printed on the screen. variable length stores the length of the string str[100].

Although I am getting the correct output from this program yet, I get additional characters after my output text. Please help me with this situation.

Here is the program that I have coded:-

#include//extra ouput
void main()
{
char str[100], subs[100];
int len,length = 0, pos,i = 0,j = 0;
printf(" enter the string\n");
gets(str);
printf(" enter the position\n");
scanf("%d", &pos);
printf(" enter the length\n");
scanf("%d", &len);
for( i = 0; str != '\0'; i++)
length++;
i=0;
if((pos+len-1) > length)
printf("invalid query terminating...\n");
else
{
i = pos;
for(j = 0; j
{
subs[j] = str;
i++;
}
}
printf("\n\n displaying the result\n\n");
i = 0;
puts(subs);
}

this program produces extra characters like ?^ at the end of the output text.

Replies

  • Shalini051
    Shalini051
    Hi,
    You have not terminated the substring(subs) after extracting it from the original string(str).Thats why the junk characters.....
  • rahul69
    rahul69
    proffy
    hello friends,
    this program produces extra characters like ?^ at the end of the output text.
    First of all, it is better to put your code in [ code ] tags as it helps in reading the code,
    Now coming to your code:
    You should add a null character after the creation of substring.
    This should solve your problem, but if u still face some problem, we are happy to help👍😁...

You are reading an archived discussion.

Related Posts

hello friends, I recently went through some proverbs that were published in a personality development magazine and remained completely alien to some of them these include good wine needs no...
According to a recent post by Silicon India, some of the technologies we have enjoyed and used since a long period of time are on the verge of getting exhausted...
CEans, Yesterday, CrazyEngineers experienced the longest unplanned downtime in the history of CE. To make the matters worse, it also blocked our official email IDs during whole the time. Though...
The war between Facebook’s Graph Search and the ones on Google, Yelp, and LinkedIn with people-powered, connections-based search is a BIG one. For the past decade or two, Google has...
Dear All, Can anybody tell me who is the supplier OR manufacturer of Single Phase VFD?