How can I store String value in char array in C? - c

I have char array to store string values. I wanted to store the value of a string variable into the char array.
char Password[30];
char User[2];
int i;
for(i=0; i<5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password,30);
}
I wanted to input the values for the array and it should throw a buffer overflow but I couldn't do it. How can I do it?

This should work for you:
#include <stdio.h>
#include <string.h>
int main() {
char Password[30];
char User[5][30];
int i;
for(i = 0; i < 5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password);
}
for(i = 0; i < 5; i++)
printf("Password %d: %s\n", i+1, User[i]);
return 0;
}
The second for loop is to show the output and that every thing is stored right!

Related

Split array by space into words

I got this code right here, which works.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "Hello how are you";
char newString[10][10];
int i,j,ctr;
printf("\n\n Split string by space into words :\n");
printf("---------------------------------------\n");
j=0; ctr=0;
for(i=0;i<=(strlen(str1));i++)
{
// if space or NULL found, assign NULL into newString[ctr]
if(str1[i]==' '||str1[i]=='\0')
{
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
}
else
{
newString[ctr][j]=str1[i];
j++;
}
}
printf("\n Strings or words after split by space are :\n");
for(i=0;i < ctr;i++)
printf(" %s\n",newString[i]);
return 0;
}
but instead of char str1[100] I want to use an array of sentenceschar str1[2][100]
Meaning
char str1[2][100] = {"Hello how are you","I'm good, thanks"}
And these two sentences (or more) I want to be separated in separate words
char str1[100] = {"Hello","how","are","you"};
Actually, this is from a project for school, in which from a file, i have to store each sentence ended by '.'
If there is another way to store each sentece directly as words instead of sentences, it would be great help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LSIZ 128
#define RSIZ 10
void yodificacio(char* arr[], int index[], int n)
{
char* temp[n];
// arr[i] should be present at index[i] index
for (int i=0; i<n; i++){
temp[index[i]] = arr[i];
}
// Copy temp[] to arr[]
for (int i=0; i<n; i++)
{
arr[i] = temp[i];
index[i] = i;
}
}
int main()
{
int i = 0;
char *arr[] = {"Hey","there","how","are","you","all","today","idk"}; **Here I want the input to be char str1[2][100] = {"Hello how are you", "Im good thanks}, instead of char arr[] ...**
int index[] = {0,2,1,4,5,3,6,7};
int n = sizeof(arr)/sizeof(arr[0]);
yodificacio(arr, index, n);
printf("Reordered array is: \n");
for (int i=0; i<n; i++)
printf ("%s ", arr[i]);
return 0;
return 0;
}
Add an outer loop to process each sentence in the array.
int main()
{
char str1[2][100] = {"Hello how are you","I'm good, thanks"} ;
char newString[10][10];
int i,j,ctr;
printf("\n\n Split string by space into words :\n");
printf("---------------------------------------\n");
j=0; ctr=0;
for (int k = 0; k < sizeof(str1) / sizeof(str1[0]); k++) {
for(i=0;i<=(strlen(str1));i++)
{
// if space or NULL found, assign NULL into newString[ctr]
if(str1[k][i]==' '|| str1[k][i]=='\0')
{
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
}
else
{
newString[ctr][j]=str1[k][i];
j++;
}
}
}
printf("\n Strings or words after split by space are :\n");
for(i=0;i < ctr;i++)
printf(" %s\n",newString[i]);
return 0;
}

rearranging a char array with strncpy in C

I am trying to change the sorting of a the arr list which could consist of zero, one, two as the inputted and stored values for arr. The stringreplace function is meant to shift every single element by one so the new sorting would be one, two, zero. I am trying to replace the elements with one another by using the strncpy function but I think it is a bit faulty, how could i fix this?
strncpy function
char stringreplace( char a[], int b){
for(int j = 0; j > b -1; j++){
strncpy(a[j], a[j+1], sizeof(a));}
for(int j = 0; j > b; j++){
printf("%s",a[j]);}
}
main function
int main()
{
char input[100];
char arr[100]= {0};
int number;
printf("Input the number of strings: ");
scanf("%d", &number);
for(int i= 0; i < number; i++){
printf("Input the number of strings: ");
scanf("%s", input);
arr[i] = input;
}
stringreplace(arr, number);
return 0;
}
You may consider allocating strings dynamically, assigning a pointer for each string into an array words, and then rotating each pointer in the array to the left.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void lrot_words(char *words[], int n);
int main(void)
{
char *p, word[100], *words[100];
int i, num_words;
printf("Enter the number of words: ");
scanf("%d", &num_words);
for(i = 0; i < num_words; i++){
printf("Enter a word: ");
scanf("%s", word);
if ((p = malloc(strlen(word) + 1)) == NULL) {
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
words[i] = strcpy(p, word);
}
lrot_words(words, num_words);
for (i = 0; i < num_words; i++) {
printf("%s\n", words[i]);
}
return 0;
}
void lrot_words(char *words[], int n)
{
char *temp = words[0];
int i;
for (i = 0; i < n - 1; i++) {
words[i] = words[i+1];
}
words[i] = temp;
}

Store & display user input in an array in C

I am trying to display and print a word from the user and store it into my array which is called
char word[20]
But I am having trouble. I know we use a "for loop" to scan it into the array but I keep going in circles and I believe the problem is with the i < 20.
I researched this and found that the answers to this are extremely experienced and I need a really basic way of doing it without the extra stuff. So all I want is to get word from the user, store it and print it onto the screen.
Can someone help without experienced code?
Code in C
char getWord(char word[]);
int main()
{
char word[20];
getWord(word);
return 0;
}
char getWord(char word[])
{
int i;
printf("Enter a word: ");
for (i = 0; i < 20; i++)
{
scanf(" %c", &word[i]);
}
return word;
}
All you want is
#include <stdio.h>
int main() {
char word[20];
scanf("%s", word); // Read and store
printf("%s\n", word); // Print
return 0;
}
You can use fgets and puts to read and write a string.
#include<stdio.h>
#define MAX 20
int main()
{
int ar[MAX], i, count;
fgets(ar, MAX, stdin); //it will accept whitespaces as well
puts(ar); //displaying entered string
return;
}
if you want to read via characters, ending character should be set to null character for it to be string.
char getWord(char word[]);
int main()
{
char word[20]
getWord(word);
printf("%s\n", word);
return 0;
}
char getWord(char word[])
{
int i;
char c;
printf("Enter a word: ");
for (i = 0; i < 19; i++)
{
scanf("%c", &c);
if ( c == '\n' )
break;
word[i]=c;
}
word[i]='\0';
return word;
}

String to int array

I've worked on this for awhile but I keep getting either {0,0,0,0,0}, or {2751685, 2751685, etc} or {57,58,59,60,etc}
void getGuess(int guess[], int length) {
char thisGuess[length];
int i=0;
printf("Enter your guess.\n");
scanf("%s", &thisGuess);
for(i=0; i<length; i++) {
printf("the guess = %d\n",(int)thisGuess[i]) ;
guess[i] = (int)(thisGuess)-48;
printf("%d ", guess[i]);
}
}
I want to enter a string, 12345, and get it so
guess[0] = 1
guess[1] = 2
guess[2] = 3
etc
suggestions on my code?
The length of an char array usually isn't equal to the length of the C-style string it contains.
void getGuess(int guess[], int length) {
char thisGuess[length];
size_t i = 0;
printf("Enter your guess.\n");
scanf("%s", thisGuess); // a char *, rather than a char (*)[length] is expected
size_t guessLength = strlen(thisGuess);
for(i = 0; i < guessLength; i++) {
printf("the guess = %d\n", thisGuess[i]);
guess[i] = thisGuess - '0';
printf("%d ", guess[i]);
}
}
Alternatively, add
if(!isdigit(thisGuess[i]))
continue;
to avoid potential out-of-bound access.

2D array of strings

I'm trying to set up a search for names in a code, so I'm doing a 2D array to store the names. however I'm not getting the desired output from doing this...
#include <stdio.h>
#include <string.h>
main ()
{
char name [4][20], string [20];
int count;
for (count = 0; count <3; count ++)
{
printf ("Enter your name \n ");
scanf ("%s", &string);
strcpy (name [count], string);
}
for (count = 0; count <3; count ++)
{
printf ("%s \n \n");
}
return 0;
}
#include <stdio.h>
#define N_NAME 4
#define NAME_LENGTH 20
int main ()
{
char names[N_NAME][NAME_LENGTH];
// input
int i;
for(i = 0; i<N_NAME ; i++) {
printf("Enter your name: ");
scanf("%s", names[i]);
}
// output
for(i = 0; i<N_NAME ; i++) {
printf("%s\n", names[i]);
}
return 0;
}
Please check this. It should be what you're looking for.
Few things to point out in your original code:
There's no need for "string" since you've stored what you need in the 2D char array.
Use "i" instead of count, which is better for writing clean code.
To make the %s in printf work, you have to give corresponding variable (pointer).
Try this.
#include <stdio.h>
#include <string.h>
main ()
{
char name [4][20], string [20];
for (int row = 0; row <4; row ++)
{
printf ("%s \n \n");
for (int col = 0; col <20; col ++)
{
printf ("Enter your name \n ");
scanf ("%s", &string);
strcpy (name [row][col], string);
}
}
return 0;
}

Resources