Thread: print alternate characters in string

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    73

    print alternate characters in string

    i want to write program to print alternate characters in string
    i have tried following code but it prints first character at infinte times
    whats wrong in below code?

    Code:
    #include <stdio.h>
    
    
    int main()
    {
      char s[100];int i,l;
      printf("Enter string ");
      scanf("%s",s);
      for(i=0;i<=strlen(s);i+2)
        printf("%c",s[i]);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Well, of course it prints the same character infinite times, you never change the value of "i". You'd want "i += 2", not "i + 2". Also, you don't want "i" to be equal to "strlen(s)" because then you'd print the null character '\0' that sits in the end of the string.
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    687
    Code:
    scanf("%s",s);
    If I enter:
    Code:
    This is a test.
    s will only contain, "This". You should use fgets() to input the entire string. Please remember that fgets() will also bring in the '\n' newline character at the end of the string entered.

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    73
    thx for reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print several ASCII characters and get the value
    By YannB in forum C Programming
    Replies: 9
    Last Post: 11-20-2013, 01:34 PM
  2. Print string with escape characters
    By koplersky in forum C Programming
    Replies: 6
    Last Post: 03-10-2013, 09:46 AM
  3. alternate to string streams
    By MicroFiend in forum C++ Programming
    Replies: 9
    Last Post: 04-30-2008, 02:03 AM
  4. how to print characters of own language?
    By kantze in forum C Programming
    Replies: 4
    Last Post: 10-17-2006, 12:22 PM
  5. alternate to string.data()
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 06-07-2004, 12:59 PM

Tags for this Thread