how to read strings which are in multiple lines and to print characters in even digit and characters in odd digit seperated by a space in each stri

how to print each character of a string in python
write a python function to insert a string in the middle of a string.
python string exercises
write a program to print every character of a string entered by user in a new line using loop
python program to count the number of characters in a string.
how to print every other letter in a string python
for ch in string python
python count total characters in string

This is my C Code

int main() {
    int n,i,j,k;
    char ch[10][10000];
    char c;
    scanf("%d",&n);
    for(i=0;i<=n;i++){
        while((j<10000)&&(c=getchar()!='\n')){
        ch[i][j]=c;}}
    for(i=0;i<n;i++)
    {
        for(k=0;k<strlen(ch[j]);k+=2)
        {
            printf("%c",ch[i][j]);
        }printf("");
        for(k=1;k<strlen(ch[j]);k+=2)
        {
            printf("%c",ch[i][j]);
        }printf("\n");
    }
    return 0;
}

I expect output of "pavan" as pvn aa but output is "terminated due to timeout


Lots wrong with your code. First off, you don't initialise j with any value. And on each time you read a line of text, you don't reset it to 0. Or in fact ever change the value.

You don't check that n is 10 or less. And you also leave a \n in the input buffer, so the first line of text you read in, isn't what you're expecting. You'd be better off using fgets to read in the entire line and then using sscanf to extract out the number.

This loop reads in one too many lines...which might be a way around you not clearing the input before reading in lines, but doesn't fix the problem.

for(i=0;i<=n;i++)

And then you have this line and it's corresponding one for the odd numbers...

for(k=0;k<strlen(ch[j]);k+=2)

j is still pointing at some undefined value. But you shouldn't be using j, you should be using i.

for(k=0;k<strlen(ch[i]);k+=2)

And then inside the loop you don't use k at all, so it's pretty pointless. Also using strlen() like that means that for time around the loop, it re-calculates the length of the string.

After all the various bugs are fixed your code would look like this...

#include <stdio.h>
#include <string.h>

int main() {
    int n,i,j;
    char ch[10][10000];

    fgets(ch[0],10000,stdin);
    sscanf(ch[0],"%d",&n);
    if((n>10)||(n<1)) {
        printf("Invalid number\n");
        return 0;
    }

    for(i=0;i<n;i++) {
        fgets(ch[i],10000,stdin);
        if(strchr(ch[i],'\n')) {
            *(strchr(ch[i],'\n'))='\0';
        }
    }

    for(i=0;i<n;i++) {
        for(j=0;j<strlen(ch[i]);j+=2) {
            printf("%c",ch[i][j]);
        }
        printf(" ");
        for(j=1;j<strlen(ch[i]);j+=2) {
            printf("%c",ch[i][j]);
        }
        printf("\n");
    }
    return 0;
}

7. Strings, Length¶. The len function returns the number of characters in a string: This loop traverses the string and displays each letter on a line by itself. print string.​lowercase print string.uppercase print string.digits The constant string.​whitespace contains all the whitespace characters, including space, tab (\t), and newline (\n). C program to read and print string using gets and puts function. This program first takes a string as input from user using gets function and stores it in a character array inputString. The advantage of using gets function is that, it can read string containing white cpace characters but gets can only read one string at a time.


in while((j<10000)&&(c=getchar()!='\n')) ch[i][j]=c;}} 'j' is not initialized before and not modified in the loop

In case by chance the random value of 'j' is < 10000 you will enter your string but it is saved in a random location corrupting the memory, and probably producing a segmentation fault

Independently of that you do not finish the input strings, when you read '\n' put a '\0' in ch[i][j]

Python Data Type: String - Exercises, Practice, Solution, Write a Python program to count the number of characters (character frequency) separated by a space and swap the first two characters of each string. a comma separated sequence of words as input and prints the unique words Write a Python function to reverses a string if it's length is a multiple of 4. In this article, we will read n strings from the input and store it in array and print the length of each string in the array. Submitted by Abhishek Pathak, on October 24, 2017 In this article, we will create a C program that will be based on Strings. We will read "n" number of strings, which will be specified by the user and store it in 2D array.


Here are some issues:

int main() {
    int n,i,j,k;      
    char ch[10][10000];
    char c;
    scanf("%d",&n);
    for(i=0;i<=n;i++){                            // Use < instead of <=
                                                  // j is not initialized
        while((j<(10000-1))&&(c=getchar()!='\n')){    // missing () around c=getchar()
        ch[i][j]=c;}}                             // j is not incremented 
                                                  // string never terminated
    for(i=0;i<n;i++)
    {
        for(k=0;k<strlen(ch[j]);k+=2)             // j should be i
        {
            printf("%c",ch[i][j]);                // j should be k
        }
        printf("");                               // missing space
        for(k=1;k<strlen(ch[j]);k+=2)             // j should be i
        {
            printf("%c",ch[i][j]);                // j should be k
        }printf("\n");
    }
    return 0;
}

fixing that like:

#define MAX 10000

int main() {
    int n,i,j=0,k;
    char ch[10][MAX];
    char c;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        j = 0;
        while((j < (MAX-1)) && ((c=getchar()) != '\n')){
            ch[i][j++]=c;
        }
        ch[i][j++]='\0';  // Terminate string 
    }
    for(i=0;i<n;i++)
    {
        for(k=0;k<strlen(ch[i]);k+=2)
        {
            printf("%c",ch[i][k]);
        }
        printf(" ");
        for(k=1;k<strlen(ch[i]);k+=2)
        {
            printf("%c",ch[i][k]);
        }printf("\n");
    }
    return 0;
}

result in:

Input: 1pavan

Ouput: pvn aa

BTW:

Notice that there is no newline between the number and the string in the input! If you put a newline there, the text string will be empty. This is because scanf leaves the newline in the buffer.

Also notice that you don't check the return value of scanf to make sure that you got a valid number. So if the input is abc (i.e. no number to start with) the variable n is uninitialized which may lead to seg fault.

Either check the scanfreturn value and handle errors or at least initialize like: n = 0;

Python String, In Python, Strings are arrays of bytes representing Unicode characters. Strings in Python can be created using single quotes or double quotes or even triple quotes. Quotes: I'm a Geek and I live in a world of "Geeks" Creating a multiline String: Geeks String.isdigit(), Returns “True” if all characters in the string are digits,  Print characters and their frequencies in order of occurrence Given a string str containing only lowercase characters. The problem is to print the characters along with their frequency in the order of their occurrence and in the given format explained in the examples below.


Strings, The second statement extracts the character at index position 1 from the fruit to the first character in the string, printing each letter on a separate line, except One common task is to remove white space (spaces, tabs, or newlines) from the Read the documentation of this method at docs.python.org/library/string.html and  C program to print a string using various functions such as printf, puts. It terminates with '\0' (NULL character), which is used to mark the end of a string.


Chapter 7 – Pattern Matching with Regular Expressions, Replace the last four print() function calls in isPhoneNumber.py with the following​: For example, a \d in a regex stands for a digit character—that is, any single While there are several steps to using regular expressions in Python, each step Also, the extra spaces inside the multiline string for the regular expression are  Python does not support a character type, these are treated as strings of length one, also considered as substring. We use square brackets for slicing along with the index or indices to obtain a substring.


[PDF] Regular Expressions: The Complete Tutorial, In this book, regular expressions are printed between guillemots: or “character string” is used by programmers to indicate a sequence of whitespace character followed by a digit. read a file line by line, and apply the regular expression separately to each because it is easy to mix up this term with “multi-line mode”. While traversing, if a space character is encountered, put a NULL in that position and print the remaining string just after the NULL character. Repeat this until the loop is over and when the loop ends, print the string, the %s will make the printing of characters until it encounters the first NULL character.