How to add a character after each word in a string?


TreyZept

So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string.

Get this "Hello? World?" for example. I think I'm getting more difficult myself by trying to solve it this way:

add(char *s, char o, char c){
    int i, j = 0;
    for (i = 0; s[i] != '\0'; i++) {
        if (s[i] != o) {
            s[j] = s[i];
        }
        else {
            s[j] = c;
        }
        j++;
    }
}  

 add(str, ' ','?');
 printf("\n%s", str);

This will read "Hello? World" without spaces. Right now, the only way I've seen this work is to move everything after the first "?" one to the right while also putting the "W" in place of a space and the "?" at the end. But for longer strings I don't see myself doing this.

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

int main(void) {
    char text[] = "Hello World";

    for(char* word = strtok(text, " .,?!"); word; word = strtok(NULL, " .,?!"))
        printf("%s? ", word);

    return 0;
}

Sample output

Success #stdin #stdout 0s 4228KB
Hello? World? 

IDEOne link

Related


How to capitalize the first character of each word in a string

WillfulWizard: Is there a function built into Java that capitalizes the first character of each word in a string without affecting other words? example: jon skeet->Jon Skeet miles o'Brien-> Miles O'Brien(B is still uppercase, this excludes title case) old mcdo

How to add the position of each character of a word in a list

Asma Mekki: I am trying to add the position of each character of a word to a list. Each line contains a character \t and a digit. Words are separated by the character "x". Also, I have to ignore the character "z". My input is an ArrayList. example: ArrayList (

How to get the first character of each word in a string?

username I want this: var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from the string, but don't know how to fix this. Vienwu You can try the

Regex - add a character before/after each word

B4b4j1 Using Notepad++ and the replace function, I try to add a sign " + " or " [ " before each word in my list. Listing example: blue car big red car little red car Eco-friendly car little green car I am looking for the following results: +blue+car +red+car+s

How to capitalize the first character of each word in a string?

Abishek I've come across a problem statement where I have to capitalize the first character in a sentence and capitalize the words that follow it. I came up with a solution using regular expressions, but had to use two regular expressions to get the job done.

How to capitalize the first character of each word in a string

WillfulWizard: Is there a function built into Java that capitalizes the first character of each word in a string without affecting other words? example: jon skeet->Jon Skeet miles o'Brien-> Miles O'Brien(B is still uppercase, this excludes title case) old mcdo

How to add "^" after each character in a string

boss I have a similar string abc123that I want to insert ^after each character in the string . I've tried messing around with the code below, but it seems this only works for numbers, not letters. Test = abc123 MsgBox, % RegExReplace(Test, "s).*?(\d{1}).*?(?=\

How to add a character after each word in a string?

TreyZept So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string. Get this "Hello? World?" for example. I think I'm getting more diff

How to add the position of each character of a word in a list

Asma Mekki: I am trying to add the position of each character of a word to a list. Each line contains a character \t and a digit. Words are separated by the character "x". Also, I have to ignore the character "z". My input is an ArrayList. example: ArrayList (

How to get the first character of each word in a string?

username I want this: var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from the string, but don't know how to fix this. Vienwu You can try the

How to add the position of each character of a word in a list

Asma Mekki: I am trying to add the position of each character of a word to a list. Each line contains a character \t and a digit. Words are separated by the character "x". Also, I have to ignore the character "z". My input is an ArrayList. example: ArrayList (

Add a space after each character in a string JavaScript

User 10938973 How can i convert the text Testto T e s ti know it can probably be done with regex but i don't understand how i can do it with javascript in python (i understand it well) it can be done with a for loop like print(" ".join(a for a in "Test")) But

How to get the first character of each word in a string?

username I want this: var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from the string, but don't know how to fix this. Vienwu You can try the

How to get the first character of each word in a string?

username I want this: var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from the string, but don't know how to fix this. Vienwu You can try the

Regex - add a character before/after each word

B4b4j1 Using Notepad++ and the replace function, I try to add a sign " + " or " [ " before each word in my list. Listing example: blue car big red car little red car Eco-friendly car little green car I am looking for the following results: +blue+car +red+car+s

How to capitalize the first character of each word in a string?

Abishek I've come across a problem statement where I have to capitalize the first character in a sentence and capitalize the words that follow it. I came up with a solution using regular expressions, but had to use two regular expressions to get the job done.

Regex - add a character before/after each word

B4b4j1 Using Notepad++ and the replace function, I try to add a sign " + " or " [ " before each word in my list. Listing example: blue car big red car little red car Eco-friendly car little green car I am looking for the following results: +blue+car +red+car+s

How to add "^" after each character in a string

boss I have a similar string abc123that I want to insert ^after each character in the string . I've tried messing around with the code below, but it seems this only works for numbers, not letters. Test = abc123 MsgBox, % RegExReplace(Test, "s).*?(\d{1}).*?(?=\

How to add a character after each word in a string?

TreyZept So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string. Get this "Hello? World?" for example. I think I'm getting more diff

Java - Add a character before each word in a string

IOS I'm looking for a function that adds a character before each identified word in a given string. For example: when I pass a string " (var1 + var2-var3 + 30)%var4", I have to expect " ($var1 + $var2-$var3 + 30)%$var4". In a given string, only words must be r

How to add an extra character after searching for a word

Andy K I have the following field names demelog voyapro naisjdf naismc decoide decoccm travide travccm equiccm mariccm Below is an example of my file. There are more columns before the first fck=83... fck=83;fcv=naismc;fcv=naisjdf;fck=83;fmd=1422811694,;fmd=1

How to capitalize the first character of each word in a string

WillfulWizard: Is there a function built into Java that capitalizes the first character of each word in a string without affecting other words? example: jon skeet->Jon Skeet miles o'Brien-> Miles O'Brien(B is still uppercase, this excludes title case) old mcdo

How to add a character after each word in a string?

TreyZept So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string. Get this "Hello? World?" for example. I think I'm getting more diff

How to add the position of each character of a word in a list

Asma Mekki: I am trying to add the position of each character of a word to a list. Each line contains a character \t and a digit. Words are separated by the character "x". Also, I have to ignore the character "z". My input is an ArrayList. example: ArrayList (

How to get the first character of each word in a string?

username I want this: var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from the string, but don't know how to fix this. Vienwu You can try the

How to get the first character of each word in a string?

username I want this: var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from the string, but don't know how to fix this. Vienwu You can try the

Regex - add a character before/after each word

B4b4j1 Using Notepad++ and the replace function, I try to add a sign " + " or " [ " before each word in my list. Listing example: blue car big red car little red car Eco-friendly car little green car I am looking for the following results: +blue+car +red+car+s

Regex - add a character before/after each word

B4b4j1 Using Notepad++ and the replace function, I try to add a sign " + " or " [ " before each word in my list. Listing example: blue car big red car little red car Eco-friendly car little green car I am looking for the following results: +blue+car +red+car+s

How to capitalize the first character of each word in a string?

Abishek I've come across a problem statement where I have to capitalize the first character in a sentence and capitalize the words that follow it. I came up with a solution using regular expressions, but had to use two regular expressions to get the job done.