To combine First , middle and last name of a person.
int main()
{
int i,j,k;
char first_name[11]="Gursheesh";
char middle_name[10]="Singh";
char last_name[10]="Chawla";
char name[30];
for(i=0;first_name!='\0';i++)
{
name[i]=first_name[i];
}
name[i]=' ';
i++;
for(j=0;middle_name!='\0';j++)
{
name[i+j]=middle_name[j];
}
name[i+j]=' ';
j++;
for(k=0;last_name!='\0';k++)
{
name[i+j+k]=last_name[k];
}
name[i+j+k]='\0';
printf("%s",name);
}
this is the code
the compiler at run time shows nothing
I cannot find the mistake,help me.
is it having some loop continue this infinity or some logical misktake.
All your loops are infinite:
for(i=0;first_name!='\0';i++)
condition first_name!='\0' is always true. You should check for first_name[i] != '\0':
for(i=0; first_name[i] != '\0'; i++)
By the way, to copy strings it's better to use strcpy function.
Cleaner approach using pointers and storing inputs in an array.
#include <stdio.h>
int main (void)
{
char result[30];
char *pntResult = result;
char *appendStr (char *dest, char *strToAppend);
char *name[3] = { "FirstName", "MiddleName", "LastName" };
int i;
for ( i = 0; i < 3; ++i )
pntResult = appendStr (pntResult, name[i]);
*(pntResult - 1) = '\0';
printf ("%s\n", result);
return 0;
}
char *appendStr (char *dest, char *strToAppend)
{
while ( *strToAppend )
*dest++ = *strToAppend++;
*dest++ = ' ';
return dest;
}
Here is the correct code.
#include <stdio.h>
int main(){
int i,j,k;
char first_name[11]="Gursheesh";
char middle_name[10]="Singh";
char last_name[10]="Chawla";
char name[30];
for(i=0;first_name[i]!='\0';i++)
{
name[i]=first_name[i];
}
name[i]=' ';
i++;
for(j=0;middle_name[j]!='\0';j++)
{
name[i+j]=middle_name[j];
}
name[i+j]=' ';
j++;
for(k=0;last_name[k]!='\0';k++)
{
name[i+j+k]=last_name[k];
}
name[i+j+k]='\0';
printf("%s",name);
}
Related
I am trying to write a program without using string library in C which can replace the spaces in string with 'XXX'.
I have done this much but did not getting idea after this.
char buff[100];
int i;
char *my_func(char *arr){
for( i=0;arr[i]!='\0';i++){
if(arr[i]==' '){
buff[i]='X';
break;
}
else{
buff[i]=arr[i];
}
}
buff[i+1]='X';
buff[i+2]='X';
return buff;
}
int main()
{
char arr[]="This is my string";
my_func(arr);
printf("%s",buff);
return 0;
}
You need to track the indices for your source and destination buffers separately, since encountering a space in the source string will cause the resulting string to grow disproportionately.
Those character assignments should be inside inside the loop.
Don't forget to NUL terminate your destination buffer.
Avoid global variables when possible.
#include <stdio.h>
char *replace(char *dest, char *src) {
size_t j = 0;
for (size_t i = 0; src[i]; i++) {
if (src[i] == ' ') {
dest[j++] = 'X';
dest[j++] = 'X';
dest[j++] = 'X';
} else
dest[j++] = src[i];
}
dest[j] = '\0';
return dest;
}
int main(void) {
char arr[] = "This is my string.";
char buffer[256];
replace(buffer, arr);
printf("[%s] --> [%s]\n", arr, buffer);
}
Output:
[This is my string.] --> [ThisXXXisXXXmyXXXstring.]
I'm trying to tokenize a string without using a strtok().
When I run characters of string, it will print in each line.
For instance, when I run:
printfTokens("Hello from other side!");
The output should be:
Hello
from
other
side!
As I'm just learning C, I'm stuck for hours on how to implement this program. So far, I only know the basics and playing around with not (still haven't learned any calloc, malloc, etc).
So far I have this code, but the output does not print anything.
#include <stdio.h>
#include <string.h>
#define MAX_WORD 100
void printfTokens(char *inputString) {
int i;
/*int inputStringLength;
for(i = 0; inputString[i] != '/0'; i++) {
inputStringLength++;
}*/
while(inputString[i] != '\0') {
char testing[MAX_WORD];
while(inputString[i] != ' ') {
testing[inputString[i]]++;
i++;
}
printf("%s", testing);
i++;
}
}
int main() {
printfTokens("TESTING ONE! TWO! THREE!");
return 0;
}
You do not initialize the variable i.
while(inputString[i] != '\0') can be written while(inputString[i]).
testing[inputString[i]]++ makes sense to count the number of occurrences of a given character from inputString, but it does not make sense to print it. You may want to do something like:
while(1)
{
char testing[MAX_WORD], *t=testing;
while(inputString[i]&&(inputString[i]!=' '))
*t++=inputString[i++];
if (t>testing) printf("%s", testing);
if (!inputString[i]) break;
i++;
}
It would be better to name MAX_WORD_LENGTH instead of MAX_WORD.
These are a few problems in your code.
Sample tokenization function.
size_t tokenize(const char *inputString, const char *delim, char **argv, size_t maxtokens)
{
size_t ntokens = 0;
char *tokenized = strdup(inputString);
if(tokenized)
{
argv[0] = tokenized;
while(*tokenized)
{
if(strchr(delim, *tokenized))
{
*tokenized = 0;
ntokens++;
if(ntokens == maxtokens - 1)
{
break;
}
argv[ntokens] = tokenized + 1;
}
tokenized++;
}
}
return ntokens + 1;
}
int main()
{
char *tokens[10];
size_t ntokens = tokenize("TESTING ONE! TWO! THREE!", " ", tokens , 10);
for(size_t i = 0; i < ntokens; i++)
{
printf("Token[%zu] = `%s`\n", i, tokens[i]);
}
free(tokens[0]);
return 0;
}
https://godbolt.org/z/znv8PszG6
I'm trying to creat a program that removes all occurences from a character that I choose from a certain string and also returs the number of characters that were removed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIMV 10
int eliminar(char texto[], char ch, char novoTexto[]){
int a=0,i;
for(i=0; texto[i] != '\0';i++){
texto[i] = texto[i+a];
novoTexto[i]=texto[i];
if(novoTexto[i]==ch){
novoTexto[i] = '\0';
a++;
}
}
return a;
}
int main()
{
char frase[]="Uma arara torta!";
char res[50];
printf("%s\n",frase);
printf("%d\n",eliminar(frase,'r',res));
printf("%s\n",res);
return 0;
}
When I run the program, it returns:
Uma arara torta!
3
Uma a
What I wanted to return is:
Uma arara torta!
3
Uma aaa tota!
It's a silly question but I can't find the mistake that I made. Thank you!
Putting '\0' means to terminate the string there.
You have to skip adding characters to eliminate.
Try this:
int eliminar(char texto[], char ch, char novoTexto[]){
int a=0,i,j=0;
for(i=0; texto[i] != '\0';i++){
if(texto[i]==ch){
a++;
}else{
novoTexto[j]=texto[i];
j++;
}
}
novoTexto[j]='\0';
return a;
}
Putting '\0' means to terminate the string there.
check this:
int eliminator(char * texto, char ch, char * novoTexto) {
int i=0;
while(*texto != '\0') {
if (*texto == ch) {
texto++;
++i;
} else {
*novoTexto++ = *texto++;
}
}
*novoTexto = '\0';
return i;
}
int main(int arg, char **argv) {
char frase[] = "Uma arara torta!";
char res[50];
printf("%s\n",frase);
printf("%d\n",eliminator(frase, 'r', res));
printf("%s\n",res);
}
I've been tryring to solve this problem for hours now.
void replaceLetters(char *text, char original, char new_char)
{
text = "Randoi";
for(int i = 0; i != '\0'; i++){
if(text[i] == original){
text[i] = new_char;
}
}
if I print out text in this function it's correct, but in the other function where this function is called the text doesn't change and i know there's something wrong with my pointers.
Please give me a hint. Tanks a lot.
Please see my code.
#include <stdio.h>
void replaceLetters(char *text, char original, char new_char)
{
for(int i = 0; text[i] != '\0'; i++)
{
if(text[i] == original)
{
text[i] = new_char;
}
}
}
int main()
{
char text[] = "Randoi";
replaceLetters(text,'n','T');
printf(text);
return 0;
}
The condition of for loop is "for(int i = 0; text[i] != '\0'; i++)"
Your function definition does not make a sense at least because you overwrote the first parameter
text = "Randoi";
with the address of a string literal and the condition in the for loop
for(int i = 0; i != '\0'; i++){
is incorrect. That is the for loop never will make iterations.
You should not use the type int for the index and the function should return the result string.
The function can be declared and defined the following way
char * replaceLetters(char *text, char original, char new_char)
{
for ( char *p = text; *p; ++p )
{
if ( *p == original ) *p = new_char;
}
return text;
}
Pay attention to that you may not use the function to change a string literal. Any attempt to change a string literal results in undefined behavior.
Here is a demonstrative progran.
#include <stdio.h>
char * replaceLetters(char *text, char original, char new_char)
{
for ( char *p = text; *p; ++p )
{
if ( *p == original ) *p = new_char;
}
return text;
}
int main(void)
{
char s[] = "character";
puts( s );
puts( replaceLetters( s, 'a', 'A' ) );
return 0;
}
The program output is
character
chArActer
#include "stdafx.h"
#include "stdio.h"
void repl(char *string, char oldc, char newc);
char text[60] = { "I am going to replace the character a with the character i";
char newc = 'b';
char oldc = 'a';
int main()
{
void repl(char *string, char oldc, char newc);
return 0;
}
void repl(char *string, char oldc, char newc)
{
int i = 0;
for (i = 0; i < *string; i++)
{
if (*(string + i) == oldc)
{
*(string + i) == newc;
}
}
printf("%s", string);
}
I am trying to replace the character a with the character b.
I know how to do this without using pointers but I am not too sure when it comes to pointers.
The prototype I was given was:
void repl(char *string, char oldc, char newc);
This does what you're after. I seriously recommend reading up on some basic C programming topics, such as calling functions and declaring char arrays. In the meantime...
#include "stdafx.h"
#include "stdio.h"
void repl(char *string, char oldc, char newc);
char text[60] = "I am going to replace the character a with the character i";
char newc = 'i';
char oldc = 'a';
int main()
{
repl(text, oldc, newc);
return 0;
}
void repl(char *string, char oldc, char newc)
{
int i = 0;
for (i = 0; string[i]; i++)
{
if (string[i] == oldc)
{
string[i] = newc;
}
}
printf("%s", string);
}