Thread: Help, made a program to slice a string, not working.

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    17

    Help, made a program to slice a string, not working.

    Code:
     #include <stdio.h>int main() {
        char s[1000];
          scanf("%s", s);//input for string
          int a, b, i;//a is upper limit and b lower limit
          scanf("%d", &a);//getting a
          scanf("%d", &b);//getting b
          for(i=b; i<b; i++){
            printf("%s", s[i]);//printing sliced string
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int a, b, i;//a is upper limit and b lower limit
    So how about
    int toIndex, fromIndex, i;

    Then check whether your code still makes sense.
    Single letter identifiers are OK for loop subscripts such as i,j.
    But everything else should have a meaningful name.

    for(i=fromIndex; i<fromIndex; i++)
    I think that fails on the very first comparison.

    > printf("%s", s[i]);
    You need "%c" format to print single characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    The program runs after changing the format specifier but nothing prints on the screen, and thanks for the tip!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you forgot to use the variable named a. That's why Salem suggested a rename to toIndex and fromIndex: it would make your mistake more obvious, e.g.,
    Code:
    for(i=fromIndex; i<fromIndex; i++){
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    Thanks! Didn't noticed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To slice or not to slice? That is the question..
    By PillsburryBarry in forum General Discussions
    Replies: 8
    Last Post: 07-12-2016, 03:38 PM
  2. program using char and string is working weirdly
    By kase20 in forum C Programming
    Replies: 3
    Last Post: 04-22-2012, 12:23 AM
  3. Home-made string class constructor
    By aab19902 in forum C++ Programming
    Replies: 4
    Last Post: 03-11-2009, 04:15 AM
  4. Program I made!
    By epidemic in forum C++ Programming
    Replies: 13
    Last Post: 04-06-2007, 09:34 PM

Tags for this Thread