Replacing ' ' with Enter in Strings

  • Thread starter Thread starter ccky
  • Start date Start date
  • Tags Tags
    Strings
Click For Summary
SUMMARY

The forum discussion focuses on modifying a C program to replace spaces in a string with newline characters. The user provided a code snippet using the `gets()` function and a `dispStr` function to display words one per line. Key issues identified include the uninitialized loop variable and the lack of logic to print characters before spaces. The solution involves initializing the loop variable and adding logic to print characters until a space is encountered, followed by a newline.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of string manipulation in C
  • Familiarity with loops and conditionals in C
  • Knowledge of standard input/output functions in C
NEXT STEPS
  • Learn about C string handling functions, specifically `strlen()` and `strcpy()`.
  • Explore the use of `fgets()` for safer string input in C.
  • Study the implementation of dynamic memory allocation in C for handling strings.
  • Investigate debugging techniques for C programs to identify and fix logical errors.
USEFUL FOR

C programmers, computer science students, and anyone looking to improve their string manipulation skills in C programming.

ccky
Messages
15
Reaction score
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
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   Reactions: 1 person

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 5 ·
Replies
5
Views
4K