Replacing ' ' with Enter in Strings

  • Thread starter ccky
  • Start date
  • Tags
    Strings
In summary, the code in the dispStr function has some errors and needs to be modified to print each word in a string on a separate line. It should also print a newline character when it encounters a space.
  • #1
ccky
15
0
The lab requires me to displace that:
Enter a string:This is a string

One word per line is:
This
is
a
string

How can I replace the ' 'with Enter.

Code:
#include <stdio.h>
#define STRSIZE 81

void dispStr(char message[STRSIZE])
{
	int i;
	
	printf("\nOne word per line is :\n");
	 
 
   while (message[i]!=' ')
   {
   	 	
   }
   
}

int main()
{
	char message[STRSIZE];
	
	printf("Enter a string: ");
	gets(message);
	dispStr(message);
	
	return 0;
}

Thanks.
 
Physics news on Phys.org
  • #2
ccky said:
The lab requires me to displace that:
Enter a string:This is a string

One word per line is:
This
is
a
string

How can I replace the ' 'with Enter.

Code:
#include <stdio.h>
#define STRSIZE 81

void dispStr(char message[STRSIZE])
{
    int i;
	
    printf("\nOne word per line is :\n");
	 
 
    while (message[i]!=' ')
    {
   	 	
    }
   
}

int main()
{
    char message[STRSIZE];
    printf("Enter a string: ");
    gets(message);
    dispStr(message);
	
    return 0;
}

Thanks.

There are a couple problems with the code in your dispStr function.
1. Your loop variable is declared, but no initialized. This will probably cause your loop to try to access memory that doesn't belong to the string. Your loop control variable (i) should be initialized to 0.
2. You don't show any code that increments the loop variable.

To answer your question, when the loop encounters a space character, it should print a newline character ('\n'). You also need logic in our loop so that it prints all of the characters that precede the space before it prints the newline character.
 
  • Like
Likes 1 person

FAQ: Replacing ' ' with Enter in Strings

How do I replace a space with Enter in a string?

To replace a space with Enter in a string, you can use the replace() method in JavaScript. This method takes two parameters: the character or string you want to replace, and the character or string you want to replace it with. In this case, you would set the first parameter to a space character (" ") and the second parameter to the "Enter" character (\n).

Can I use this method for replacing multiple spaces with Enter?

Yes, you can. The replace() method can also take regular expressions as parameters, so you can use the global flag (g) to replace all occurrences of a character or string. For example, to replace all spaces with Enter, you can use the regular expression / /g as the first parameter.

Is it possible to replace a specific number of spaces with Enter?

Yes, you can specify the number of spaces you want to replace with Enter by using the repeat() method in JavaScript. This method takes a number as a parameter and repeats the specified string the given number of times. In this case, you would use the repeat() method to repeat the "Enter" character the desired number of times, and then pass it as the second parameter in the replace() method.

Can I use this method to replace spaces with other characters, not just Enter?

Yes, you can replace spaces with any character or string using the replace() method. You just need to specify the desired character or string as the second parameter. For example, you could replace spaces with commas, dashes, or any other character you need.

Are there any other methods for replacing spaces with Enter in strings?

Yes, there are other methods you can use, depending on the language or programming platform you are using. For example, in Java, you can use the replaceAll() method, which takes a regular expression as a parameter and replaces all occurrences of the pattern with the specified string. In Python, you can use the replace() method, similar to JavaScript, but with a different syntax. It's always a good idea to research and explore different methods to find the best one for your particular needs.

Similar threads

Replies
7
Views
2K
Replies
12
Views
2K
Replies
3
Views
1K
Replies
2
Views
1K
Replies
12
Views
1K
Replies
3
Views
909
Replies
1
Views
9K
Back
Top