Member_2_6613198
asked on
String manipulation in C
This program for returning the re-arranged string where taking the first letter of a “word” and appending that letter to the end of the word with “ay” added to the end as well.
Please help me to write the return function properly instead of printing them. Currently it works with Printf commands.
Please help me to write the return function properly instead of printing them. Currently it works with Printf commands.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void Translate(const char* in, const char* out)
{
static const char* SEP = " .,?"; // word separators
// Iterate input by words
const char *sep = NULL, *word = NULL, *end = in; *temp=out;
while (sep = end, // separators from previous word end
word = &end[strspn(end, SEP)], // start of word
end = &word[strcspn(word, SEP)], // end of word
*sep) // iterate until we hit terminating zero character
{
int wordlen = (int)(end - word);
int seplen = (int)(word - sep);
if (wordlen > 0 && isalpha(word[0])) // word starts with a letter, pig it!
{
char firstletter = tolower(word[0]);
const char* suffix = (firstletter == 'a') ? "y" : "ay";
//printf("\n%s",&sep[seplen]);
//printf("\n%s",&word[1]);
printf("\n%.*s%.*s%c%s",
seplen, sep, // separators from previous word
wordlen - 1, &word[1], // word without first letter
firstletter, suffix);
}
else // not a real word, just print unchanged
{
printf("%.*s%.*s", seplen, sep, wordlen, word);
}
}
}
int main()
{
char inputBuffer[100];
char outputBuffer[200];
printf("\n Enter the string: \n");
fgets(inputBuffer, sizeof(inputBuffer), stdin);
Translate(inputBuffer,outputBuffer);
}
Can you post your input and output , Shall write the program for you !
ASKER
Example
Input = Darrin, what are you doing with 500 KG and 100 KG?
Output = arrinday, hatway reay ouyay oingday ithway 500 Gkay nday 100 Gkay?
Darrin, has become arrinday,
are become reay
Input = Darrin, what are you doing with 500 KG and 100 KG?
Output = arrinday, hatway reay ouyay oingday ithway 500 Gkay nday 100 Gkay?
Darrin, has become arrinday,
are become reay
try writing your output to txt file .
for example
char name;
int number;
FILE *f;
f = fopen("scambledtext.txt", "a");
fprintf(f, "%c\n\n", output of program);
fclose(f);
note; to add multiple output just use fprint statement for every output before closing file
for example
char name;
int number;
FILE *f;
f = fopen("scambledtext.txt", "a");
fprintf(f, "%c\n\n", output of program);
fclose(f);
note; to add multiple output just use fprint statement for every output before closing file
you should answer to phoffric's question as experts can only give little help in case of a homework question. if it is homework (or similar academical purpose) you should ask specific questions to your code rather than expect us to give a solution for the whole function.
for example, you could ask
We then could answer like,
where you pass a zero-terminated string as input and provide a buffer for the output string with the size such that the function can avoid any buffer overflow. note, the output string may not be const since you can't write on it if so.
the return value is the output length on success. if the output is NULL, the return value is the required output length. if buffer size too small, the return code is error -1 .
Sara
for example, you could ask
Which argument types and return type should I choose for the Translate function
We then could answer like,
int Translate(const char * input, char * output, int sizOutput);
where you pass a zero-terminated string as input and provide a buffer for the output string with the size such that the function can avoid any buffer overflow. note, the output string may not be const since you can't write on it if so.
the return value is the output length on success. if the output is NULL, the return value is the required output length. if buffer size too small, the return code is error -1 .
Sara
ASKER
@phoffric: it's an assignment.
Can you help me on this . I am trying to append the few characters(depends on seplen) from *sep(which is character pointer)
for (i=0;i<seplen;i++)
{
temp[j]=*sep++;
}
I am getting runtime error when I do like this. That's why I want people advise on posted code
Can you help me on this . I am trying to append the few characters(depends on seplen) from *sep(which is character pointer)
for (i=0;i<seplen;i++)
{
temp[j]=*sep++;
}
I am getting runtime error when I do like this. That's why I want people advise on posted code
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This piece code complied properly but i dont see the output it's blank
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
void piglatinize(const char* in)
{
static const char* SEP = " .,?"; // word separators
// Iterate input by words
const char *sep = NULL, *word = NULL, *end = in;
char *temp = (char*) malloc(1000*sizeof(char)+1);
int i,j=0;
while (sep = end, // separators from previous word end
word = &end[strspn(end, SEP)], // start of word
end = &word[strcspn(word, SEP)], // end of word
*sep) // iterate until we hit terminating zero character
{
int wordlen = (int)(end - word);
int seplen = (int)(word - sep);
if (wordlen > 0 && isalpha(word[0])) // word starts with a letter, pig it!
{
char firstletter = tolower(word[0]);
const char* suffix = (firstletter == 'a') ? "y" : "ay";
for (i=0;i<seplen;i++)
{
temp[j]=*sep++;
}
printf ( "\n%s",temp);
/*printf("%.*s%.*s%c%s",
seplen, sep, // separators from previous word
wordlen - 1, &word[1], // word without first letter
firstletter, suffix);*/
}
else // not a real word, just print unchanged
{
printf("%.*s%.*s", seplen, sep, wordlen, word);
}
j++;
}
}
int main()
{
piglatinize("Darrin, what are ");
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the Asker got valid corrections for the posted code and good and detailed suggestions how to solve the task. Full solutions could not be given because it was an assignment. An equal split between the contributors seems to be appropriate.
Sara
Sara