Question

this code is working perfectly fine in my compiler(DEV C++) but not in Ideone.com . It is not accepting replacement string. is there anything wrong with my logic ? May I know whats wrong with my code?

//splitting a string and replace latter part of string by another string
#include<stdio.h>
#include<string.h>
int i,count=0,loc2=0,scount=0,rcount=0,loc=0;       //scount represents counter for substring and rcount for replacement and loc from where we will split the string
char str[100],sub[100],newss[100];  //newss=new substr, rslt and rslt2=former and latter part of original string
int main()
{
    String();
    substring();
    new_string();
    concat();
    return 0;
}
void String()
{
    printf("Enter a String:\n");
    gets(str);
    printf("\nString Entered by User:\n");
    puts(str);
    printf("\nLoc Char\n");         //Creates Colums 'Char' and 'Loc'
    for(i=0;str[i]!='\0';i++)
    {
        count++;                                //Counts length of String
        printf("%d. %c\n",count,str[i]);    //Prints Characters with it its Corresponding Location
    }
    printf("\n\nLength of String: %d\n\n",count);
}
void substring()
{
    printf("Enter the locations of Characters from where substring will start and end: \n");
    scanf("%d%d",&loc,&loc2);       //stores indices of begining and end of substring
    printf("\n\nSubset formed from Existing String:\n");
    for(i=loc-1;i<loc2;i++)
    {
        scount++;
        sub[i]=str[i];              //stores substring in "sub"
        printf("%c",sub[i]);
    }
    printf("\n\nLength of substring: %d\n",scount);
}
void new_string()
{
    printf("\n\nEnter a Replacement for substring(Of Equal Length as that of substring):\n");
    fflush(stdin);
    gets(newss);
    for(i=0;newss[i]!='\0';i++)
    rcount++;
    printf("\n\nLength of New substring: %d\n",rcount); //-1 to subtract length of null char
}
void concat()
{
    if(rcount!=scount)      //to check whether replacement string and substring are of same length
    printf("\nSince length of both substrings is not same. \nHence Replacement is Not Possible\n");
    else        //Concatination of 3 substrings
    {
        printf("\nResultant String:\n");
        for(i=0;i<(loc-1);i++)
        printf("%c",str[i]);
        for(i=0;newss[i]!='\0';i++)
        printf("%c",newss[i]);
        for(i=loc2;str[i]!='\0';i++)
        printf("%c",str[i]);
    }
}
Was it helpful?

Solution

You're doing a number of strange things, and some bad things as well, one of the big problems is you're intermixing calls to gets() and scanf(), they handle new line characters differently and it's getting you in trouble.

When you call substring() it's using scanf() to get the two substring indexes, but it's leaving a newline character('\n') on stdin, then your call to gets() is reading that and using it as the string.

Change:

gets(newss);

To:

scanf(" %s", newss);

And things will work in ideone.com.

You maybe wondering why did you have this problem when you called fflush(stdin); just before reading newss. This is part of what I described as "bad things" before. fflush(stdin) should NEVER be done. This leads to undefined behavior, fflush() is well defined for stdout but not for stdin. This means calling it could flush the input buffer or it could not it depends on how that was implemented in the IDE you were using (or if it was at all). If it's not defined in the C standard, you can't assume it will work.


EDIT:
your example is using spaces in the substring you're entering so the answer is the same, but you need to use the negated scanset:

scanf(" %[^\n]", newss);   // note my examples start with a blank space
//     ^
//     |
//   here

The blank space will tell scanf() to ignore any remaining "white space" characters left on stdin, that means that left over '\n' will not be considered. However that ALSO means that if your replacement string starts with a space that space will be ignored. You can read the man page for scanf() and consider exactly what you want to use for your input string based on your assignment requirements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top