-4

I am trying to figure out what is required to obtain the text between two characters.

The program will prompt the user for the string, and then the program will prompt the user for the character.

After doing so, the program will print the text using the character as the limit.

For example:

Please insert your text: asdhello wordasd
Please insert your character: d

Your desired text: hello wor

I am trying to think about what to do, but I am clueless.

Any suggestions?

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
Akade
  • 27
  • 3

3 Answers3

2

If you are doing this in C. C store string as character array. You can access each and every character using the array index(0 to length of the string). Just go through the array and check whether you have found the character you want to find. If you found it first time then set a flag variable and if you found it second then check the flag variable whether it is second time or first time. Copy/print all character in between the first and second found occurrences. Best of luck.

Imran
  • 4,582
  • 2
  • 18
  • 37
-1

Here is the code:

#include<stdio.h>
int main (void)
{
 char text[100];
 char desiredText[100];
 char character;
 int size, begin, end, i, j;

 // to initialize as an empty string
 text[0] = '\0';
 desiredText[0] = '\0';

 printf("Please insert your text: ");
 scanf ("%[^\n]%*c", text);

 // finding the size of the text input by user
 size = 0;
 while( text[size] != '\0' )
  size++;

 printf("Please insert your character: ");
 scanf("%c", &character);

 // finding the first occurrence of the character or, if not found stop at the end of the text
 begin = 0;
 while( text[begin] != character  && text[begin] != '\0' )
  begin++;

 // checking to see if an empty string was given as text or the character was not found at all
 if( begin >= 0 && begin < size )
 {
  // checking to see if the first occurrence was at the last character of the text
  if( begin == size -1 )
  {
   printf("Only one occurence of %c as the last character, hence, nothing in between!\n", character);
  }
  else
  {
   // if all is good, finding the second occurrence of the character or, if not found, stop at the end of the text
   end = begin + 1;
   while( text[end] != character  && text[end] != '\0' )
    end++;
   // checking if both occurrences are adjacent to each other
   if( begin == end - 1 )
   {
    printf("Nothing in between!\n");
   }
   else
   {
    // manually copying the string and adding a terminating character \0 to denote the end
    for(i = begin + 1, j = 0; i < end; i++, j++)
     desiredText[j] = text[i];
    desiredText[j] = '\0';

    printf("Your desired text: %s\n", desiredText);
   }
  }
 }
 else
 {
  printf("'%c' not found in the text.\n", character);
 }
 return 0;
}

Some explanation:

  • Here, since, we cannot use string.h, we will have to do all string manipulation, such as finding length of string, copying a string to another one, searching for a character in the string and even terminating the string with a \0 to denote its end. All these things are being done in the code above manually. (See comments in code to understand.)

  • The && in the while loop, between two conditions, is used to stop the loop if either of the two conditions has occurred.

  • The way to take a string with spaces as input from user in scanf ("%[^\n]%*c", text); is taken from this answer on SO.

Community
  • 1
  • 1
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • use `fgets` to scan a line having spaces in them and then strip off the `\n` after taking the string. `scanf("%[^\n]%*c", text)` has some limitations. Alternatively in `C11`, you can also use `gets_s` – Mayukh Sarkar Feb 10 '16 at 06:42
  • Your `main` function signature is also wrong..Both `C` and `C++` standards doesn't qualify your main..Plus you have not used any functions which will require `string.h`, `stdlib.h` and `unistd.h`...Plus while scanning the string, you can find the length of it..You don't need a separate loop for that – Mayukh Sarkar Feb 10 '16 at 06:47
  • Removed the extra includes and fixed main signature. – Ahmed Akhtar Feb 10 '16 at 07:37
  • 1
    `scanf ("%[^\n]%*c", text);` reads nothing if input begins with `'\n'`, leaving `text` uninitialized and `'\n'` still in `stdin`. A poor substitute for `fgets()`. – chux - Reinstate Monica Feb 10 '16 at 16:41
  • 1
    @Mayukh Sarkar Concerning `gets_s()`: C11 recommends "Consider using `fgets` (along with any needed processing based on new-line characters) instead of `gets_s`." – chux - Reinstate Monica Feb 10 '16 at 16:44
  • I personally always have used fgets or write system call for my Unix based systems – Mayukh Sarkar Feb 10 '16 at 16:47
-1

Here is the code:

#include<stdio.h>
#include<conio.h>


int main()
{

    char string[100];  //for storing character array
    char ch;           //for storing the character given by user
    char temp='!';     //for checking each character with ch
    int count=0;       //counter for while loop
    int flag=0;        // becomes 1 after the first occurence of ch in string
                      // and increases after the subsequent occurences
    clrscr();         // used to clear screen(optional)

    printf("Enter a string\n");
    scanf("%[^\n]",string);     // read string from the user

    printf("Enter the character: ");
    scanf("%c",&ch);   // read character from the user


    while(temp!='\0')  //temp initiated with '!' so a to enter the loop
    {
        temp=string[count];
        if(temp==ch)
        {
            flag++;
        }
        else if(flag==1)
        {
            printf("%c",temp);
        }
        count++;
    }

    getch();
    return 0;
}

EXPLANATION:

Firstly, I have stored the string and character.

Then, a temporary character variable temp, to store each character of the string one by one in the loop.

Then, temp is matched against ch and when temp matches ch, flag is increased from 0 to 1 for the first time and all the subsequent characters are printed on the console, until the second time temp matches ch and then, flag is further increased, which stops the printing of further characters.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
Harsh Dave
  • 329
  • 1
  • 2
  • 13
  • Use of `gets`...Your answer should be deleted by moderator.. I am flagging it..Super Downvote for `gets`..Didn't see anything ofter seeing your gets – Mayukh Sarkar Feb 10 '16 at 14:59
  • ohhh thanks to you sir Mayukh Sarkar. I made a great mistake by using gets for reading the string. I am very thankful to you for pointing it out. I have edited it by using scanf ....thanks :-) – Harsh Dave Feb 10 '16 at 15:40
  • please see if there are any other mistakes in my code...... – Harsh Dave Feb 10 '16 at 15:43
  • Use `fgets` for C99 or less and `gets_s` for c11 to scan a string with spaces. – Mayukh Sarkar Feb 10 '16 at 15:59
  • Using `scanf("%[^\n]",string);` reads nothing if input begins with `'\n'`. IAC, the `'\n'` is still left in `stdin` to be read by the next `scanf("%c",&ch);` - certainly not the intended function. Using `scanf()` without checking its results is generally an unsafe practice. – chux - Reinstate Monica Feb 10 '16 at 16:49
  • consequently if you are in *nix systems, you can use `write(fileno(stdin), buffer, BUF_SIZE)`..It is a reentrant or async-safe function compared to `fgets` and hence will be more safe in case you are doing some signal handling. – Mayukh Sarkar Feb 11 '16 at 05:36